0

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

1 Answer 1

4

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.