views:

47

answers:

2

Greetings!

I'm doing a Form on ASP.NET MVC 2, in my view I have a TextBox for the name, and two buttons. One of the buttons is for submit, and the other one have a function in JS, that add's another textbox, and a drop down list.

In the post controller action method, how do I get all the parameters? Here is the View Code:

<body>
    <div>
        <%using (Html.BeginForm())
          { %>
          New Insurance Type Name:
          <%=Html.TextBox("InsuranceName") %>
          <div id="InsuranceDetails"/>
    </div>
   <div id="Buttons">
      <input type="button" onclick="AddFieldForm()" value="Add Field" />
      <p />
      <input type="submit" value="Submit" />
        <%} %>
  </div>
</body>
A: 

If you're dealing with an arbitrary number of items in your form post, I'd suggest encoding the post on the client side with javascript so that you're actually only sending a single item back to your controller. JSON seems like the obvious choice.

http://www.json.org/json2.js

spender
But how do I do that, if I dont know how many items I've got? Should I give them all distinct id atributes?Thank you.
StackPointer
+1  A: 

You could just use the form collection parameter on your controller and make sure your generated textboxes have unique ids.

public ActionResult SomeMethod(FormCollection formValues)
{
   foreach (string key in formValues)
   {                
        if (key.ToLower().StartsWith("form-text-"))
        {                    
           //Do Something     
        }
   }
}
OG
Thank you very much.
StackPointer