0

I want to perform validation without using Data Annotation as i have used EF in n-tier MVC 4 razor architecture. And also Data Annotations doesn't work for EF controls.You all can see my code here:

[HttpPost]
public ActionResult RegisterNewUser(CreateUser obj)
{
    if (ModelState.IsValid)
    {
        //    
    }
}

and ModalState.Isvalid doesn't work. How can I resolve this?

2
  • DataAnnotations doesn't work for EF controls, what? Commented Aug 20, 2014 at 8:49
  • Please make your question more clear. It makes no sense!! What is "Data Annotations doesn't work for EF controls"? How could you get an invalid ModelState if there is nothing that sets it? Commented Aug 20, 2014 at 9:19

1 Answer 1

1

I think what you're looking for are cases where the DataAnnotation validation will not give you the flexibility you need.

For example, I need an address which can either be selected via a dropdown or entered. If one of the entered address lines is there then all of them need to be there. That's tough to check with DataAnnotations alone.

What I've done in the past is do the validation once control is returned to the controller but before the ModelState.IsValid is checked.

This allows custom/complex validation in the controller

You can use then

    ModelState.AddModelError("PostalCode", "The postal code is missing.");

to modify the ModelState (Make it invalid) which can then return control to the view after checking ModelState.IsValid

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.