Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC - Model binding with multiple entities on the same page

Tags:

asp.net-mvc

I am wondering how to use Model Binding in a scenario where I am returning information from more than one entity on a page?

I want to display a combination of fields from two separate entities, ie Customer + Address. I am using Microsoft's DAAB and custom business entities for my model.

Any ideas?

like image 947
Dkong Avatar asked Jul 03 '09 14:07

Dkong


People also ask

Can we bind multiple models with a single view?

Handling multiple models in a single view To achieve this, refer to the following steps. First, create a new model (common for all models) and refer all other models that are to be used in the same view. Refer to the following code example. Next, create controller and pass the common model to it.

What is two way binding MVC?

So the two-way data binding means we can perform both read and write operations. In our previous article Data Binding in Spring MVC with Example, we have discussed how to write-to-variable task and in this article, we mainly focus on the read-from-a-variable task.


2 Answers

If you are trying to bind to multiple models on postback, you should try using the Bind attribute and specifying the prefixes used for each model in your arguments. In some scenarios -- where you may not be able to use separate prefixes for your model elements -- you might find this easier to do with multiple TryUpdateModel and separate whitelists rather than putting the models in the parameters.

public ActionResult Update( [Bind(Prefix="Customer")]Customer customer,
                            [Bind(Prefix="Address")]Address address )
{
   ...
}

This would assume you have a ViewModel like:

public class CustomerAddressModel
{
    public Customer Customer { get; set; }
    public Address Address { get; set; }
}

and reference it like:

<%= Html.TextBox( "Customer.Name" ) %>
...
<%= Html.TextBox( "Address.Street" ) %>

or, using TryUpdateModel,

public ActionResult Update( int id )
{
    var customer = db.Customers.Where( c => c.ID == id ).Single();

    var whitelist = new string[] { "name", "company", ... };
    if (TryUpdateModel( customer, whitelist ))
    {
        var addressWhitelist = new string[] { "street", "city", ... };
        if (TryUpdateModel( customer.Address, addressWhitelist ))
        {
            ...
        }
    }

}

In this case, your model might contain just the fields from the two different models that you are trying to update.

public class CustomerAddressModel
{
    public string Name { get; set; }
    public string Company { get; set; }
    public string Street { get; set; }
    ...
}
like image 108
tvanfosson Avatar answered Sep 20 '22 00:09

tvanfosson


The short answer is:

  1. Build your ViewModel using the Domain objects that you want information about,
  2. Then use @model<ViewModel type> as the first line in your View.

You have now strongly typed your View, and have access to all of the properties of BOTH of the Domain objects.

like image 40
Andrew McFall III Avatar answered Sep 21 '22 00:09

Andrew McFall III