c# - How to handle lots of derived model with only one controller -
i using asp.net mvc , have several model classes derived parent object. want handle of these models in 1 controller action since identical exception of data fields. want save them database. how can achieve such behavior?
example:
class parentmodel {...} class childmodel1 : parentmodel {...} class childmodel2 : parentmodel {...} class childmodel3 : parentmodel {...} class childmodel4 : parentmodel {...} public class modelcontroller : controller { //but want handle child objects //and add them automatically database. public actionresult add(parentmodel model) { db.parentmodel.add(model); } }
you should create viewmodel class such :
public class viewmodel { public childmodel1 childmodel1 { get; set; } public childmodel2 childmodel2 { get; set; } public childmodel3 childmodel3 { get; set; } public childmodel4 childmodel4 { get; set; } }
then create object of view model:
viewmodel v = new viewmodel();
now can add child model view model:
v.childmodel1 = yourchildmodel1; v.childmodel2 = yourchildmodel2; v.childmodel3 = yourchildmodel3; v.childmodel4 = yourchildmodel4;
now can pass model.
Comments
Post a Comment