MVC - Model binding with multiple entities on the same page

Last post 07-04-2009 9:02 AM by paul.vencill. 4 replies.

Sort Posts:

  • MVC - Model binding with multiple entities on the same page

    07-03-2009, 10:17 AM
    • Member
      482 point Member
    • LeMerovingian
    • Member since 03-07-2006, 2:11 PM
    • Perth, Western Australia
    • Posts 318

    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?

    Filed under:
  • Re: MVC - Model binding with multiple entities on the same page

    07-03-2009, 9:19 PM
    • Contributor
      6,613 point Contributor
    • gerrylowry
    • Member since 07-03-2008, 1:46 AM
    • alliston ontario canada
    • Posts 2,267

    My guess, something like this:

    private YourFirstTableDBEntities _dbFirst = new YourFirstTableDBEntities();
    private YourSecondTableDBEntities _dbSecond = new YourSecondTableDBEntities();

    on your .aspx page:

        Html.Encode(itemFirst.somefield)   Html.Encode(itemSecond.someotherefield)

             et cetera

     

    Regards.
    Gerry (Lowry)

    Gerry Lowry, Principal
    Ability Business Computer Services ~~ Because it's your Business, our Experience Counts!
    68 John W. Taylor Avenue
    Alliston · Ontario · Canada · L9R 0E1 · gerry.lowry@abilitybusinesscomputerservices.com

    Websites:
    http://abilitybusinesscomputerservices.com
    http://gerrylowryprogrammer.com ~~ résumé & testimonials
    http://veganoccasions.com ~~ recipes by Susan
  • Re: MVC - Model binding with multiple entities on the same page

    07-04-2009, 2:59 AM
    • Member
      482 point Member
    • LeMerovingian
    • Member since 03-07-2006, 2:11 PM
    • Perth, Western Australia
    • Posts 318

    Thanks for your reply. I am wondering how to do this if I want to display data from a collection (of which the collection comprises of data from two different entities).

    Can I do this with Model Binding?

  • Re: MVC - Model binding with multiple entities on the same page

    07-04-2009, 3:34 AM
    • Contributor
      6,613 point Contributor
    • gerrylowry
    • Member since 07-03-2008, 1:46 AM
    • alliston ontario canada
    • Posts 2,267

    I am not an expert, however, I would say yes, you can.  See http://msdn.microsoft.com/en-us/library/dd410405.aspx:
    "Models and Model Binders in MVC Applications":

    "Models are application specific, and therefore the ASP.NET MVC framework puts no restrictions on the kinds of model objects you can build."

    "A model binder in MVC provides a simple way to map posted form values to a .NET Framework type and pass the type to an action method as a parameter. Binders also give you control over the deserialization of types that are passed to action methods. Model binders are like type converters, because they can convert HTTP requests into objects that are passed to an action method. However, they also have information about the current controller context."

    Read the full article at the above link.

    See also http://nerddinnerbook.s3.amazonaws.com/Part2.htm where a one to many* foreign key relationship is set up and http://nerddinnerbook.s3.amazonaws.com/Part3.htm where the developers of NerdDinner "use LINQ to SQL to create a simple model that corresponds fairly closely to our database design".

    * in your case, it seems like you are using a one to one relationship.  I tend to think of one to one as a special case of one to many.  Keep in mind that I am not a database expert.

     

    Regards,
    Gerry (Lowry)

    Gerry Lowry, Principal
    Ability Business Computer Services ~~ Because it's your Business, our Experience Counts!
    68 John W. Taylor Avenue
    Alliston · Ontario · Canada · L9R 0E1 · gerry.lowry@abilitybusinesscomputerservices.com

    Websites:
    http://abilitybusinesscomputerservices.com
    http://gerrylowryprogrammer.com ~~ résumé & testimonials
    http://veganoccasions.com ~~ recipes by Susan
  • Re: MVC - Model binding with multiple entities on the same page

    07-04-2009, 9:02 AM
    • Contributor
      6,620 point Contributor
    • paul.vencill
    • Member since 02-01-2006, 7:57 AM
    • Gaithersburg, MD
    • Posts 1,350

    You can do this w/ ModelBinding, yes.  See  my answer to your other post about using a viewmodel to wrap two models.  You can do collection-based binding by accepting a List<yourviewmodeltype> parameter in the controller's POST action, and following the right syntax in the view.

    Here's a  good exampel of that.  http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

    You can also use helpers, in the manner that I showed in the other post; you just pass the index as a string like Phil shows.

    Help those who have helped you... remember to "Mark as Answered"
Page 1 of 1 (5 items)