ASP.Net MVC - Editing a collector is the best way with FormCollection?

This same question was asked here and an answer was given that can be followed up, but with the completion of the ASP.Net MVC framework, I wondered if there was a better solution.

If I have the following class structure, how to create a view page and more importantly, return the data back to the controller.

public class Person {


public int Id {get;set;}
  public string Name {get;set;}
  public IList<TelNos> TelNos {get;set;}
}

public class TelNos{
  public string Type {get;set;}
  public string Number {get;set;}
}

      

I understand that within the page, I could include the following (assuming a strongly typed view):

<% foreach (var telNo in Model.Product.TelNos)
               {%>
            <p><label for="telNo.Type">Type of Number</label>

                <%= Html.TextBox("telNo.Type")%>
                <%= Html.ValidationMessage("telNo.Type", "*")%>
            </p>
<p><label for="telNo.Number">Type of Number</label>

                <%= Html.TextBox("telNo.Number")%>
                <%= Html.ValidationMessage("telNo.Number", "*")%>
            </p>
            <%} %>

      

Assuming I initiated 2 TelNos objects, I could see 2 sets of text boxes in the view.

When this form is submitted back, the suggestion in the previous post was to iterate over the FormCollection in the post method:

[AcceptVerbs( HttpVerb.POST )]
public ActionResult Whatever( FormCollection form )
{
 ....
}

      

However, has a better approach now, or has additional updates to MVC provided a better solution?

Thanks Richard

0


a source to share


3 answers


Thanks for your reply. Your answer gave me an insight which I think is different from what you are suggesting.

If I include an index for an object in the view, then the model builder takes the default values ​​and assigns them to related objects. The view code looks like this:



    <% int i=0;
foreach (var telNo in Model.Product.TelNos)
                   {%>
                <p><label for="telNo.Type">Type of Number</label>

                    <%= Html.TextBox("telNo"+i.ToString()+".Type")%>
                    <%= Html.ValidationMessage("telNo"+i.ToString()+".Type", "*")%>
                </p>
    <p><label for="telNo.Number">Type of Number</label>

                    <%= Html.TextBox("telNo"+i.ToString()+".Number")%>
                    <%= Html.ValidationMessage("telNo"+i.ToString()+".Number", "*")%>
                </p>
                <%i++;
} %>

      

Richard

0


a source


There were no updates or best practices (as far as I know) for handling dynamic forms in mail. Still proven and reliable ways are to either bind data to your information or execute it through FormCollection. If you look here it might help you with data binding. OR in the latter case, you can iterate through the collection of forms by calling different values ​​with their string name. Although this may have some conflicts as they will all have the same ID

 "telNo.Type"
 "telNo.Number"

      

You may have to do some manipulation to have something like

 "telNo.Type[i]"
 "telNo.Number[i]"

      



where i is the number in sequence for this item in the list. You might as well have this something else combination of strings that generates a unique ID for that object so you can get the type and number.

 "object[i].telNo.Type" 
 "object[i].telNo.Number"

      

It really depends on how you think it is best to implement it. Sometimes getting the data binding to work in dynamic forms can be painful and it's easier to just iterate over the collection and then use something like LINQ to get the ones you want, group / etc.

+2


a source


I like the idea of ASP.NET MVC Partial View with a Form which in turn links to Model Binding to List

I think this is better for two reasons:

  • Going through the FormCollection violates the idea of ​​splitting the view form controller. The controller will have too much knowledge about how the data is displayed.
  • Writing a unit test will be painful. you need to insert all values ​​into the form controller manually.
0


a source







All Articles