6

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?

1
  • 1
    This is a duplicate, im pretty sure. Make a Model object with public properties for your Customer and Address objects. Commented Jul 3, 2009 at 14:20

3 Answers 3

21

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; }
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

+ but maybe "try/catch with UpdateModel" is better than this "nested if(TryUpdateModel" construction?
@eu-ge-ne -- maybe. It would depend on how you handle updating the model state on errors. Without actually writing the tests for the code it's hard to tell exactly what should be done.
1

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.

Comments

-4

You can use ViewData to pass multiple models to a view.

SomeControllerMethod()
{
    ...
    ViewData["Customer"] = myCustomer;
    ViewData["Address"] = myAddress;
}

And the view:

Customer name: <%= (ViewData["Customer"] as Customer).Name %>
Street: <%= (ViewData["Address"] as Address).Street %>

1 Comment

This passes multiple objects to the view, they are not models, you will not be able to access their properties through the views Model object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.