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 page
return RedirectToAction("MainDisplay");
}
return View(main.Prop1);
}
//[...] similar thing for Prop2
//Prop1 and Prop2 could perhaps share same view as its strongly
//typed to the same class
}
I believe this code all make sense until now (correct me if it's not the case) because TryValidateModel()
is validating against a model with no ValidationAttribute
.
The problem lies here, where would be the best place, or what would be the best and most elegant way to have different validation constraints for Prop1
and Prop2
while still taking advantage of TryValidateModel()
and not filling the Edit method with conditional statements and ModelState.AddModelError()
Usually you could have validation attributes in the SomeSubModel
class, but it wouldn't work in this case, because there is different constraints for each property.
Other option is that there could be Custom validation attribute in the MainModel
class, but it also wouldn't work in this case because the SomeSubModel
object is passed directly to the view and when validating has no reference to its MainModel
object.
The only left option I can think about is a ValidationModel for each property, but I am not quite what would be the best approach for this.
Solution
Here's solution I implemented, based of @MrMindor's answer.
Base ValidationModel class:
public class ValidationModel<T> where T : new()
{
protected ValidationModel() {
this.Model = new T();
}
protected ValidationModel(T obj) {
this.Model = obj;
}
public T Model { get; set; }
}
Validation Model for Prop1
public class Prop1ValidationModel:ValidationModel<SomeSubModel>
{
[StringLength(15)]
public string Name { get{ return base.Model.Name; } set { base.Model.Name = value; } }
public Prop1ValidationModel(SomeSubModel ssm)
: base(ssm) { }
}
Validation Model for Prop2
public class Prop2ValidationModel:ValidationModel<SomeSubModel>
{
[StringLength(70)]
public string Name { get{ return base.Model.Name; } set { base.Model.Name = value; } }
public Prop2ValidationModel(SomeSubModel ssm)
: base(ssm) { }
}
Action
[HttpPost]
public ActionResult EditProp1(SomeSubModel model){
Prop1ValidationModel vModel = new Prop1ValidationModel(model);
if(TryValidateModel(vModel)){
//[...] persist data
//when succesfully saved return to main display page
return RedirectToAction("MainDisplay");
}
return View(model);
}