c# - Validation Not Working in MVC4 -
i'm new mvc , i'm creating web app , i'm stuck problem. in app i've signup page , need validate given input not working current code given below
model
public class account { [required(errormessage = "user name required")] [stringlength(15, errormessage = "first name length should less 50")] public virtual string username { get; set; } [required(errormessage = "email id required")] [stringlength(35, errormessage = "email length should less 35")] [regularexpression(@"^\w+@[a-za-z_]+?\.[a-za-z]{2,3}$", errormessage = "email id not in proper format")] public virtual string emailid { get; set; } }
controller
[httppost] public actionresult signup(string username,string email) { if (modelstate.isvalid) { try { account newaccount = new account(); var userexist = newaccount.userexist(username); if (userexist == 0) { accountbl createaccount = new accountbl(); createaccount.username = username; createaccount.emailid = email;; newaccount.signup(createaccount); return view("index"); } else { return view(); } } catch { return view(); } } return view(); }
view
@using (html.beginform("signup", "account", formmethod.post)) { @html.validationsummary(true) <div> <fieldset> <legend>sign up</legend> <table> <tr> <td> @html.label("user name") </td> <td> @html.textboxfor(account => account.username) @html.validationmessagefor(account => account.username) </tr> <tr> <td> @html.label("email") </td> <td> @html.textboxfor(account => account.emailid) @html.validationmessagefor(account => account.emailid) </td> </tr> <tr> <td> <input type="submit" name="btnsubmit" value="sign up" /> </td> </tr> </table> </fieldset> </div> }
i cant find wrong in code please point out whats wrong code
looks you're not passing model in post method. instead of passing username , passwords strings, try passing model type bound view. post action method signature should this:
public actionresult signup(accountmodel model)
Comments
Post a Comment