3

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>

2 Answers 2

2

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     
        }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

https://github.com/douglascrockford/JSON-js/blob/master/json2.js

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.