ASP.NET MVC Multiple form in one page: Validation doesn't work -
i creating register login page in asp mvc , need, page has 2 models , 2 form actions. every thing ok validation. models are:
public class account_index_viewmodel { public useraccount_login_viewmodel useraccount_login_viewmodel { get; set; } public useraccount_register_viewmodel useraccount_register_viewmodel { get; set; } } public class useraccount_login_viewmodel { [required] [datatype(datatype.password)] public string pass { get; set; } [required] public string loginname { get; set; } // nickname/email/mobilephone } public class useraccount_register_viewmodel { public string nickname { get; set; } public string passw { get; set; } public string passconfirm { get; set; } public string email { get; set; } public string mobilephone { get; set; } }
and view :
@model ghafasehwebsite.models.account_index_viewmodel @{ viewbag.title = "index"; } <h2>index</h2> <div class="accountbook"> <div class="half-width"> @using (html.beginform("login", "account")) { html.enableclientvalidation(); @html.antiforgerytoken() <div class="form-horizontal"> <h4>ورود به سیستم</h4> <hr /> @html.validationsummary(true) <div class="form-group col-md-12"> @html.labelfor(model => ((ghafasehwebapi.models.useraccount_login_viewmodel)(model.useraccount_login_viewmodel)).loginname, new { @class = "control-label col-md-4" }) <div class="col-md-8"> @html.textboxfor(model => ((ghafasehwebapi.models.useraccount_login_viewmodel)(model.useraccount_login_viewmodel)).loginname, new { @class = "form-control", placeholder = "نام مستعار/ایمیل/شماره موبایل" }) @html.validationmessagefor(model => ((ghafasehwebapi.models.useraccount_login_viewmodel)(model.useraccount_login_viewmodel)).loginname) </div> </div> <div class="form-group col-md-12"> @html.labelfor(model => ((ghafasehwebapi.models.useraccount_login_viewmodel)(model.useraccount_login_viewmodel)).pass, new { @class = "control-label col-md-4" }) <div class="col-md-8"> @html.textboxfor(model => ((ghafasehwebapi.models.useraccount_login_viewmodel)(model.useraccount_login_viewmodel)).pass, new { @class = "form-control" }) @html.validationmessagefor(model => ((ghafasehwebapi.models.useraccount_login_viewmodel)(model.useraccount_login_viewmodel)).pass) </div> </div> <div class="form-group"> <div class="col-md-offset-4 col-md-10"> <input type="submit" value="ورود" class="btn btn-primary" /> </div> </div> </div> } </div> <div class="half-width"> @using (html.beginform("register","account")) { @html.antiforgerytoken() <div class="form-horizontal"> <h4>ثبت نام در سیستم</h4> <hr /> @html.validationsummary(true) <div class="form-group col-md-12"> @html.labelfor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).nickname, new { @class = "control-label col-md-4" }) <div class=" col-md-8"> @html.textboxfor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).nickname, new { @class = "form-control" }) @html.validationmessagefor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).nickname) </div> </div> <div class="form-group col-md-12"> @html.labelfor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).passw, new { @class = "control-label col-md-4" }) <div class=" col-md-8"> @html.textboxfor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).passw, new { @class = "form-control" }) @html.validationmessagefor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).passw) </div> </div> <div class="form-group col-md-12"> @html.labelfor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).passconfirm, new { @class = "control-label col-md-4" }) <div class=" col-md-8"> @html.textboxfor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).passconfirm, new { @class = "form-control" }) @html.validationmessagefor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).passconfirm) </div> </div> <div class="form-group col-md-12"> @html.labelfor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).email, new { @class = "control-label col-md-4" }) <div class=" col-md-8"> @html.textboxfor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).email, new { @class = "form-control" }) @html.validationmessagefor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).email) </div> </div> <div class="form-group col-md-12"> @html.labelfor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).mobilephone, new { @class = "control-label col-md-4" }) <div class=" col-md-8"> @html.textboxfor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).mobilephone, new { @class = "form-control" }) @html.validationmessagefor(model => ((ghafasehwebapi.models.useraccount_register_viewmodel)model.useraccount_register_viewmodel).mobilephone) </div> </div> <div class="form-group"> <div class="col-md-offset-4 col-md-10"> <input type="submit" value="ثبت نام" class="btn btn-success" /> </div> </div> </div> } </div> </div> <div> @html.actionlink("back list", "index") </div> @section scripts { @scripts.render("~/bundles/jqueryval") }
and controller:
[httpget] public actionresult index() { return view(new account_index_viewmodel()); } [httppost] [validateantiforgerytoken] public actionresult login(useraccount_login_viewmodel model) { if (modelstate.isvalid) { if (dataprovider.loginuser(model, modelstate, request, session)) { return redirecttoaction("index", "home"); } } return view("index"); } [httppost] [validateantiforgerytoken] public actionresult register(useraccount_register_viewmodel model) { if (modelstate.isvalid) { if (dataprovider.registeruser(model, modelstate, request, session)) { return redirecttoaction("index", "home"); } } return view("index"); }
you should know server side validation works fine client side asleep. so, suggest?
when validation doesn't work mean? press login button empty username/password , doesn't show required error? if , created new project model view controller , worked !!! validation works.
Comments
Post a Comment