KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Problem I know there is a lot of ways of doing Model validation within MVC, and there is quite a lot of documentation regarding this topic. However, I'm not quite sure what's the best approach for validating properties of the Model which are "Sub Model" of same type . Keep in mind the following I still want to to take profit of the TryUpdateModel/TryValidateModel methods Each of these "sub models" have strongly typed views There is one strongly typed view for the MainModel class that renders the overall display view It might sound a little confusing but i'll throw in some code to clarify. Take as example the following classes: MainModel: class MainModel{ public SomeSubModel Prop1 { get; set; } public SomeSubModel Prop2 { get; set; } } SomeSubModel: class SomeSubModel{ public string Name { get; set; } public string Foo { get; set; } public int Number { get; set; } } MainModelController: class MainModelController{ public ActionResult MainDisplay(){ var main = db.retrieveMainModel(); return View(main); } [HttpGet] public ActionResult EditProp1(){ //hypothetical retrieve method to get MainModel from somewhere var main = db.retrieveMainModel(); //return "submodel" to the strictly typed edit view for Prop1 return View(main.Prop1); } [HttpPost] public ActionResult EditProp1(SomeSubModel model){ if(TryValidateModel(model)){ //hypothetical retrieve method to get MainModel from somewhere var main = db.retrieveMainModel(); main.Prop1 = model; db.Save(); //when succesfully saved return to main display pa
Tags (comma-separated)
Save Edits
Cancel