A controller with an unknown number of parameters?

Hello!

I am making a form on ASP.NET MVC 2, in my opinion, I have a TextBox for the name and two buttons. One of the buttons is for sending, and the other has a function in JS that adds another text box 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>
+3
source share
2 answers

You can simply use the form collection option on your controller and make sure that the text fields you create have unique identifiers.

public ActionResult SomeMethod(FormCollection formValues)
{
   foreach (string key in formValues)
   {                
        if (key.ToLower().StartsWith("form-text-"))
        {                    
           //Do Something     
        }
   }
}
+2
source

Source: https://habr.com/ru/post/1738971/


All Articles