Returning Multiple Models in 1 View in ASP.NET MVC3

Many mvc developers where asking on how to return multiple Models on a single view. Well its not that complex,  and here is how I did it.

First : Create the models

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcProject.Models {
     public class FirstModel : IEnumerable<FirstModel>, IList<FirstModel> {
         public string Title { get; set; }
         public string Message { get; set; }
         ...
     }
     public class SecondModel : IEnumerable<SecondModel>, IList<SecondModel> {
         public string Title { get; set; }
         public string Message { get; set; }
         ...
     }

     /*Strongly typed wrapper model*/
     public class WrapperModel {
          FirstModel firstModel;
          SecondModel secondModel;
          public WrapperModel() {
                 firstModel = new FirstModel();
                 secondModel = new SecondModel();
          }
     }
}

Second : Create our controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcProject.Models;

namespace MvcProject.Controllers {
     public class HomeController : Controller {
          public ActionResult Index() {
                WrapperModel wrapperModel = new WrapperModel();
                return View(wrapperModel);
          }
     }
}

Finally : Access our model through Razor

@model MvcProject.Models.WrapperModel
<div>
     @{
                foreach (MvcProject.Models.FirstModel fm in ViewData.Model.firstModel) {
                    <div>
                        <div id="SponsorNameContainer">@fm.Title</div>
                        <div id="SponsorMessageContainer">@fm.Message</div>
                    </div>
                }
            }
</div>
<div>
     @{
                foreach (MvcProject.Models.SecondModel sm in ViewData.Model.secondModel) {
                    <div>
                        <div id="SponsorNameContainer">@sm.Title</div>
                        <div id="SponsorMessageContainer">@sm.Message</div>
                    </div>
                }
            }
</div>

Advertisements

28 thoughts on “Returning Multiple Models in 1 View in ASP.NET MVC3

Add yours

  1. This seems to be not a working code. I am getting error while compiling. The error is ‘Multiplemodels.Models.FirstModel’ does not implement interface member ‘System.Collections.Generic.ICollection.Add(Multiplemodels.Models.FirstModel’). Also, in the View, I was not getting the below code:

    foreach (MvcProject.Models.FirstModel fm in ViewData.Model.firstModel)
    The ‘ViewData.Model.firstModel is having error, saying that it is inaccesible due to its protection level.

    Any idea why it is going wrong?

  2. Hi Rajamaran,

    The first and second model must implement IEnumerable interface. The above code is incomplete but the concept is already there, notice the ellipses. If you want a full working source, i can send you a sample project just send me your email.

  3. public class Album
    {

    [Display(Name=”Id”)]
    public Int64 Id{get;set;}

    [Display(Name=”Album Name”)]
    public string Name{get;set;}

    [Display(Name=”Description”)]
    public string Album_Desc{get;set;}

    [Display(Name=”Creation Date”)]
    public DateTime Album_CreateDate{get;set;}
    }

    public class Tracker
    {
    [Display(Name=”Id”)]
    public Int64 Id{get;set;}

    [Display(Name=”Tracker Name”)]
    public string Tracker{get;set;}

    [Display(Name=”Create Date”)]
    public DateTime CreateDate{get;set;}

    [Display(Name=”Tracker Comment”)]
    public string Tracker_Comment{get;set;}
    }

    // Now combine the both Model into one…

    public class AlbumTracker
    {
    [Display(Name=”Album Definition”)]
    public Album AlbumDescription{get;set}

    [Display(Name=”Tracker Definition”)]
    public Tracker TrackerDescription{get;set;}
    }

    // Now called the AlbumTracker in View controller

    @model [YourProjectName].Model.AlbumTracker

    @Html.LabelFor(t=>t.Album.Id)
    @Html.LabelFor(t=>t.Album.Id)

    @Html.LabelFor(t=>t.Tracker.Description)
    @Html.TextBoxFor(t=>t.Tracker.Description)

    // like that you define

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: