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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jaganathan BantheswaranPosted Oct 26, 2013, 9:32 AM
sourav mondal 0Posted Oct 25, 2013, 2:02 PM
http://dotnetawesome.blogspot.com/2013/09/2-model-in-1-view-in-mvc-4.html
Sasi ReddyPosted Oct 21, 2013, 3:27 AM
Loading vaious models in one view is not good practice .So i am following partial views.
Jaganathan BantheswaranPosted Oct 18, 2013, 12:17 PM
Sasi ReddyPosted Oct 18, 2013, 7:45 AM
Jaganathan BantheswaranPosted Oct 18, 2013, 6:22 AM
//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
}