开发者

Controller with unknown number of parameters?

开发者 https://www.devze.com 2022-12-24 19:47 出处:网络
Greetings! I\'m doing a Form on ASP.NET MVC 2, in my view I have a TextBox for the name, and two buttons.

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 i开发者_Python百科n 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>


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     
        }
   }
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消