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>
good Idea..
thanks
How you you go about editing multiple models related by a foreign key?
It’s not returning the mulitple model. it’s just returning one model that has two nested models
“not valid in the given context” error for
foreach (MvcProject.Models.FirstModel fm in ViewData.Model.firstModel) {
row.
I have same problem ViewData.Model.firstModel is error how did you solve it?
Hi, very clean and simple solution… nice!! i will test right now.
…and this other article is very helpfull http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.net-mvc-applications
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?
francorobles, please send me the full working source to correspond at live . com. Thank you.
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.
virali.summi@gmail.com
Please send compleate code .Its urgent.
I request you to please give me a full source code. I am get the data using linq to sql
can you send me the full solution to cl_yee99@hotmail.com
Thanks!
francorobles, please send me the full working source to correspond at live . com. Thank you.
good
Ya i also need the complete source. Since am also getting the same result. I meant error. My mail id is
aravindhsbtech@gmail.com
please send me source code ………
Hello francorobles: can you send me the full working source to psdevelopr12 at gmail . com
Thanks
How Can I call Stored Procedures in view (Razor)?
Please can any one tell me
Would you please send the full source to trozky@hotmail.com. Appreciate it
Really ? even with “foreach (MvcProject.Models.FirstModel fm in ViewData.Model.firstModel) {“
For those asking the full source code http://francorobles.com/PublicFiles/BiliranProvince2.zip. There is now a better approach for this. Which I will blog probably within this week. Im currently busy with oDesk projects.
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
Do you have any video of that? I’d love to find
out more details.