ASP.NET MVC form handling an unknown number of inputs

I create an internal page that allows trusted users to manually change the settings through the form. The inputs to this setting are a list of settings (of unknown size), each of which has a specific list of values. Then the user can select a value for all or a subset of the parameters.

I tried to illustrate this with my current model for presentation.

    public class SetupModel
    {
        public List<SetupParameter> Parameters { get; set; }
    }

    public class SetupParameter
    {
        public string ParameterName { get; set; }

        // list with text=paramvalue, value=paramvalueid 
        public SelectList ParameterValueList { get; set; } 
        // id of the selected parametervalue if any
        public int? SelectedParameterValueID { get; set; }
    }

My current attempt to pretend for this is:

<% using (Html.BeginForm("Update", "Parameters") {%>
...
<% foreach( var parameter in Model.Parameters ) { %>
            <div><%: parameter.ParameterName %></div>
            <div><%: Html.DropDownListFor(x => parameter.SelectedParameterValueID, parameter.ParameterValueList, "Please select") %></div>

<% } %>
...

, , , . , , :)

+3
2

FormCollection:

public ActionResult Submit(FormCollection formCollection)
{
     //Iterate form collection to get fields

     return View();
}
+4

Phil Haack : .

, (ParameterName), , .

0

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


All Articles