Multiple models in single view and single controller
I have a view with multiple models,but i need to know how to write the code in controller to save the data.Please tell me its very urgent
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Oct 26, 2013, 5:32 AM EDT
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Oct 18, 2013, 2:22 AM EDT
//View model
class ExampleViewModel
{
public ExampleModel ExModel1 { get; set }
public ExampleModel ExModel2 { get; set }
}
//Action in Controller
public ActionResult Index()
{
var viewModel = new ExampleViewModel{
ExModel1 = new ExampleModel(),
ExModel2 = new ExampleModel()
};
return View(viewModel);
}
//View
In your POST method,
[HTTPPost]
public void Save(ExampleViewModel viewmodel)
{ //Access models like this
viewmodel.ExModel1.SomeProeprty
viewmodel.ExModel2.SomeProperty
}
Sasi ReddyRank #4,458 · 342 reputationPosted Oct 18, 2013, 3:45 AM EDT
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Oct 18, 2013, 8:17 AM EDT
Sasi ReddyRank #4,458 · 342 reputationPosted Oct 20, 2013, 11:27 PM EDT
Loading vaious models in one view is not good practice .So i am following partial views.
sourav mondal 049 reputationPosted Oct 25, 2013, 10:02 AM EDT
http://dotnetawesome.blogspot.com/2013/09/2-model-in-1-view-in-mvc-4.html