asp.net mvc - MVC not pulling through validation -


i have following view code:

@using (html.beginform("login", "press", formmethod.post)) { <fieldset>     <legend>user registration</legend>     <div>         @html.textboxfor(model => model.fullname)         @html.validationmessagefor(model => model.fullname)     </div>     <div>         @html.textboxfor(model => model.company)         @html.validationmessagefor(model => model.company)     </div>     <div>         @html.textboxfor(model => model.emailaddress)         @html.validationmessagefor(model => model.emailaddress)     </div>     <div>         @html.checkboxfor(model => model.joinmailinglist)         please check box recieve seasonal book pdf , monthly newsletter     </div>     <p>         <input type="submit" value="proceed" />     </p> </fieldset> } 

and here model:

public class userviewmodel {     [required(errormessage = "please enter name.")]     [maxlength(100)]     public string fullname { get; set; }      [required(errormessage = "please enter name of company.")]     [maxlength(50)]     public string company { get; set; }      [required(errormessage = "please enter email.")]     [datatype(datatype.emailaddress)]     [regularexpression(@"^(([a-za-z0-9]+_+)|([a-za-z0-9]+\-+)|([a-za-z0-9]+\.+)|([a-za-z0-9]+\++))*[a-za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-za-z]{2,6}$", errormessage = "please enter valid email address.")]     [maxlength(255)]     public string emailaddress { get; set; }      public bool joinmailinglist { get; set; } } 

the problem when click on 'proceed' button, none of validation occurs. posts action no validation performed on it? have perform inside controller?

here controller code:

public class presscontroller : controller {     //     // get: /press     public viewresult index()     {         return view();     }      //     // get: /press/login     public viewresult login()     {         return view();     }      [httppost]     public actionresult login(userviewmodel userviewmodel)     {         return redirecttoaction("index", "press");     } } 

make sure action posting takes view model argument:

[httppost] public actionresult press(userviewmodel model) {     // @ stage validation has been performed during     // process of model binding , in      // modelstate if model vaild:      if (!modelstate.isvalid)      {         // validation failed => redisplay view user         // can fix errors.          // note calling modelstate.isvalid doesn't trigger validation         return view(model);     }      // @ stage know validation passed => processing     // , redirect      return redirecttoaction("success"); } 

or people use tryupdatemodel method allows perform model binding triggers validation:

[httppost] public actionresult press() {     var model = new userviewmodel();     // validation triggered @ stage , method     // return true or false based on result of validation     if (!tryupdatemodel(model))     {         // validation failed => redisplay view user         // can fix errors.          return view(model);     }      // @ stage know validation passed => processing     // , redirect      return redirecttoaction("success"); } 

and if want enable client side validation, make sure following 2 scripts referenced in page:

<script src="@url.content("~/scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@url.content("~/scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -