Flood Gravemind
Flood Gravemind

Reputation: 3803

Custom validating a Model object in MVC

I I need to validate some thing in mvc in my controller I have to

@Html.ValidationMessageFor(model => model.somestring)// in the view

if (model.string = some condition)
ModelState.AddModelError("somestring", "String cannot be empty");// in the controller

but if in my view I have a custom object like

@Html.ValidationMessageFor(model => model.someobject.somestring)// in the view

how do I validate it? Is the following syntax correct?

if (model.someobject.somestring = some condition)
ModelState.AddModelError("somestring", "String cannot be empty");// in the controller

Upvotes: 0

Views: 85

Answers (1)

CodingIntrigue
CodingIntrigue

Reputation: 78525

You need to make sure the full path to your property is used when specifying your key:

ModelState.AddModelError("someobject.somestring", "String cannot be empty);

Upvotes: 4

Related Questions