Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Model Binder in ASP.NET MVC Beta Support List<T>?

Take the example classes below. I want to display the customer and two addresses (from a LIST) on a form. Does the model binder in MVC beta support this or will I have to write my own custom binder?

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<Address> Addresses { get; set; }

    public Customer()
    {
        Addresses = new List<Address>();
    }

}

public class Address
{
    public int Line1 { get; set; }
    public int Line2 { get; set; }
    public int City { get; set; }
    public int State { get; set; }
    public int Zip { get; set; }
}

How would you code the fields? Like this?

<!-- some HTML formatting -->
<%= Html.TextBox("customer.address.line1", ViewData.Customer.Address[0].Line1)%>
<!-- some more HTML formatting -->
<%= Html.TextBox("customer.address.line1", ViewData.Customer.Address[1].Line1)%>
<!-- end of HTML form formatting -->
like image 739
37Stars Avatar asked Jan 02 '09 17:01

37Stars


People also ask

What are model binders in MVC?

Model binding is a simplistic way to correlate C# code with an HTTP request. The model binding applies to transforming the HTTP request data in the query's form string and form collection of the action method parameters. We can consider these parameters to be primitive type or complex type.

How do you bind a model to view in MVC?

Model binding is a well-designed bridge between the HTTP request and the C# action methods. It makes it easy for developers to work with data on forms (views), because POST and GET is automatically transferred into a data model you specify. ASP.NET MVC uses default binders to complete this behind the scene.

Does MVC use data binding?

asp.net mvc supports data binding. You can bind data to some model and post it back when the form is submitted.


2 Answers

I've never tried it, but see this post, it's about model binding to a list, maybe it can help you.

like image 166
Eduardo Campañó Avatar answered Oct 25 '22 02:10

Eduardo Campañó


Use MvcContrib's NameValueDeserializer to make it simpler. Let's assume that your page derives from ViewPage<Customer>. You can do this:

<%= Html.TextBox("Address[0].Line1", ViewData.Model.Address[0].Line1)%>
<%= Html.TextBox("Address[1].Line1", ViewData.Model.Address[1].Line1)%>

And this:

public ActionResult Save([Deserialize]Customer customer)

And the customer will be deserialized from the form post with the address collection populated. Your indexes do not have to be in sequence -- this supports cases where you want to remove rows on the client side before the post occurs.

In the case that you are deserializing something from the view data dictionary (instead of the Model), then the syntax is like [Deserialize("customer")], where "customer" is the prefix.

You might find this blog post interesting and relevant.

like image 35
Tim Scott Avatar answered Oct 25 '22 02:10

Tim Scott