views:

27

answers:

1

Im facing problem when trying to apply data annotation. In my case im passing FormCollection in controller

[HttpPost]         
public ActionResult Create(string Button, FormCollection collection)
{
if (ModelState.IsValid)
      {
      }
else
      {
      }
}

and in ModelState.IsValid condition always have true value. Although i have left some blank fields in View. Also EnableClientValidation() is also applied in View for client side validation but its not working. what may be the problem

A: 

Your view must be strongly typed, and the parameters of your Create function must contain an object to hold your Model, not a generic FormCollection. So if you have a model of name MyClass that you have annotated, then you should have that as the parameter. Otherwise, how will the model binder know what class should it check your form against?

Palantir
thanks for accepting it
Palantir