Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
3.20/5 (2 votes)
See more:
Dear Frnds,

I am developing Purchase order screen in mvc.. I have two models 'MRHdr' and 'MRDtl' How to use two models in single view. I am trying 3 days please help me...

Eg. Textbox values save in 'MRHdr' and grid values save in 'MRDtl'

What I have tried:

Models: public class MRHdr
{
[Key]
public int Id { get; set; }
public string MRNo { get; set; }
public DateTime MRDate { get; set; }
public string Remarks { get; set; }
}
public class MRDtl
{
[Key]
public int Id { get; set; }
public string MRNo { get; set; }
public string IRateCode { get; set; }
public string ItemCode { get; set; }
public int QtyRequested { get; set; }
}
View Model:
public class VwModel
{
public MRHdr MRHdr { get; set; }
public MRDtl MRDtl { get; set; }
}
Controller:
public ActionResult Create()
{
MRHdr objMRHdr = new MRHdr() { MRNo = "", MRDate = DateTime.Now };
MRDtl objMRDtl = new MRDtl() { IRateCode = "", ItemCode = "", QtyRequested = 0 };
List model = new List();
model.Add(new VwModel { MRHdr = objMRHdr, MRDtl = objMRDtl });

return View(model);
}

View:
@model Telecom.ViewModel.VwModel
@using (Html.BeginForm("Create", "MatReq", FormMethod.Post))
{

@{Html.RenderPartial("_MRHdrs",Model.MRHdr);}
@{Html.RenderPartial("_MRDtls", Model.MRDtl);}

}

Partial View:1
@model List< Telecom.Models.MRDtl >

Partial View:2
@model Telecom.Models.MRHdr
Posted
Updated 24-Jul-16 20:29pm
v5
Comments
Philippe Mori 23-Jul-16 9:18am    
Use code block for your code...

By the way, it is very easy to find answer of such question using Google...

An article has already been written explaining many ways of accomplish this.

With a Google search of
mvc multiple models c#
, it was easy to find the information you need. You can try that yourself: Google[^]

Multiple Models in a View in ASP.NET MVC 4 / MVC 5[^]

That question also has already been asked on Stack Overflow:`
asp.net mvc - Multiple models in a view - Stack Overflow[^]

I would usually make another model that contains both models as properties and then create partial view for each part. This work well if each part are essentially independant (like combining 2 pages into one).
 
Share this answer
 
Comments
Vivek.anand34 25-Jul-16 1:02am    
Same Error: u see above view, that given model is correct or not..

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Telecom.ViewModel.VwModel]', but this dictionary requires a model item of type 'Telecom.ViewModel.VwModel'.
Vivek.anand34 25-Jul-16 1:07am    
public ActionResult Create()
{
MRHdr objMRHdr = new MRHdr() { MRNo = "", MRDate = DateTime.Now };
MRDtl objMRDtl = new MRDtl() { IRateCode = "", ItemCode = "", QtyRequested = 0 };
List model = new List();
model.Add(new VwModel { MRHdr = objMRHdr, MRDtl = objMRDtl });

return View(model);
}
try like this


C#
public ActionResult Create()
       {
           MRHdr objMRHdr = new MRHdr() { /* initialise the properties*/ };
           MRDtl objMRDtl = new MRDtl() { /* initialise the properties*/  };
           VwModel model = new VwModel{ MRHdr = objMRHdr, MRDtl = objMRDtl };
           return View(model);
       }
       public ActionResult GetList()
       {
           MRHdr objMRHdr = new MRHdr() { /* initialise the properties*/ };
           MRDtl objMRDtl = new MRDtl() { /* initialise the properties*/  };
           List<VwModel> model = new List<VwModel>();
           model.Add( new VwModel{ MRHdr = objMRHdr, MRDtl = objMRDtl }); // sample
           model.Add(new VwModel { MRHdr = objMRHdr, MRDtl = objMRDtl }); // sample
           return View(model);
       }
 
Share this answer
 
Comments
Vivek.anand34 23-Jul-16 2:31am    
Thanks.. But Error like this:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Telecom.ViewModel.VwModel]', but this dictionary requires a model item of type 'Telecom.ViewModel.VwModel'.
Karthik_Mahalingam 23-Jul-16 2:37am    
use @model Telecom.ViewModel.VwModel for getting only one item
and @model List< telecom.viewmodel.vwmodel > for list of model
Vivek.anand34 23-Jul-16 2:36am    
How to initialize properties in object for eg.
Karthik_Mahalingam 23-Jul-16 2:38am    
like this
{ new MRDtl { IRateCode="", ItemCode = "", QtyRequested=0 } };
Vivek.anand34 23-Jul-16 3:00am    
Same Error: u see above view, that given model is correct or not..

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Telecom.ViewModel.VwModel]', but this dictionary requires a model item of type 'Telecom.ViewModel.VwModel'.
Why not use two partial views and use one model for each.
 
Share this answer
 
Comments
Vivek.anand34 25-Jul-16 2:32am    
I have updated my overall code above.. Error: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Telecom.ViewModel.VwModel]', but this dictionary requires a model item of type 'Telecom.ViewModel.VwModel'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900