asp.net mvc - Validate required model only -
i'm using 2 models, login & signup model in view.
public class login { [required(errormessage ="user id required.")] public string userid { get; set; } [required(errormessage ="password required")] public string password { get; set; } } public class signup { [required (errormessage ="user id required")] public string userid { get; set; } [required (errormessage ="name required")] public string name { get; set; } [required (errormessage ="mail id required")] public string mailid { get; set; } [required(errormessage ="password required")] public string password { get; set; } [required(errormessage ="confirm password required")] [compare (nameof(password), errormessage ="password not match")] public string confirmpassword { get; set; } }
when click login button, validates both models. how validate model separately?
used following codes in controller
public actionresult index() { return view(); } [httppost] public actionresult index(string command, login login) { if (command == "signup") { return redirecttoaction("contact"); } else { if (modelstate.isvalidfield("userid") && modelstate.isvalidfield("password")) { return redirecttoaction("about"); } } return view(); }
index.cshtml view code:
@using (html.beginform()) { @html.antiforgerytoken() @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="holderform"> <div class="col-md-6"> <div class="form-horizontal"> <h4>login</h4> <hr /> <div class="form-group"> <div class="col-md-10"> @html.editorfor(o => o.login.userid, new { htmlattributes = new { @class = "loginentry", @placeholder = "user id" } })<br> @html.validationmessagefor(o => o.login.userid,"", new {@class= "loginvalidation" } ) </div> </div> <div class="form-group"> <div class="col-md-10"> @html.editorfor(o => o.login.password, new { htmlattributes = new { @class = "loginentry", @placeholder = "password" } })<br> @html.validationmessagefor(o => o.login.password,"", new { @class = "loginvalidation" }) @viewbag.posted </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> @viewbag.posted </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="login" id="btn_login" name="command" class="btn btn-default" /> </div> </div> </div> </div> <div class="col-md-6"> <div class="form-horizontal"> <h4>signup</h4> <hr /> @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-group"> <div class="col-md-10"> @html.editorfor(o => o.signup.userid, new { htmlattributes = new { @class = "loginentry", @placeholder = "user id" } })<br> @html.validationmessagefor(o => o.signup.userid,"", new { @class = "loginvalidation" } ) </div> </div> <div class="form-group"> <div class="col-md-10"> @html.editorfor(o => o.signup.name, new { htmlattributes = new { @class = "loginentry", @placeholder = "name" } })<br> @html.validationmessagefor(o => o.signup.name, "", new { @class = "loginvalidation" }) </div> </div> <div class="form-group"> <div class="col-md-10"> @html.editorfor(o => o.signup.mailid, new { htmlattributes = new { @class = "loginentry", @placeholder = "mail id" } })<br> @html.validationmessagefor(o => o.signup.mailid, "", new { @class = "loginvalidation" }) </div> </div> <div class="form-group"> <div class="col-md-10"> @html.editorfor(o => o.signup.password, new { htmlattributes = new { @class = "loginentry", @placeholder = "password", @type = "password" } })<br> @html.validationmessagefor(o => o.signup.password, "", new { @class = "loginvalidation" }) </div> </div> <div class="form-group"> <div class="col-md-10"> @html.editorfor(o => o.signup.confirmpassword, new { htmlattributes = new { @class = "loginentry", @placeholder = "confirm password", @type = "password" } })<br> @html.validationmessagefor(o => o.signup.confirmpassword, "", new { @class = "loginvalidation" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type= "submit" value="signup" id="btn_login" name="command" class="btn btn-default" /> </div> </div> </div> </div> </div> }
above code place on view using 2 different model.
please , suggest me learn asp.net (beginner level)?
you add login
, signup
in 1 form
@using (html.beginform()){ ... }
try this
<div class="holderform"> @using (html.beginform()) { @html.antiforgerytoken() @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="col-md-6"> <div class="form-horizontal"> <h4>login</h4> <hr /> <div class="form-group"> <div class="col-md-10"> @html.editorfor(o => o.login.userid, new { htmlattributes = new { @class = "loginentry", @placeholder = "user id" } })<br> @html.validationmessagefor(o => o.login.userid,"", new {@class= "loginvalidation" } ) </div> </div> <div class="form-group"> <div class="col-md-10"> @html.editorfor(o => o.login.password, new { htmlattributes = new { @class = "loginentry", @placeholder = "password" } })<br> @html.validationmessagefor(o => o.login.password,"", new { @class = "loginvalidation" }) @viewbag.posted </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> @viewbag.posted </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="login" id="btn_login" name="command" class="btn btn-default" /> </div> </div> </div> </div> } @using (html.beginform()) { @html.antiforgerytoken() @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="col-md-6"> <div class="form-horizontal"> <h4>signup</h4> <hr /> @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-group"> <div class="col-md-10"> @html.editorfor(o => o.signup.userid, new { htmlattributes = new { @class = "loginentry", @placeholder = "user id" } })<br> @html.validationmessagefor(o => o.signup.userid,"", new { @class = "loginvalidation" } ) </div> </div> <div class="form-group"> <div class="col-md-10"> @html.editorfor(o => o.signup.name, new { htmlattributes = new { @class = "loginentry", @placeholder = "name" } })<br> @html.validationmessagefor(o => o.signup.name, "", new { @class = "loginvalidation" }) </div> </div> <div class="form-group"> <div class="col-md-10"> @html.editorfor(o => o.signup.mailid, new { htmlattributes = new { @class = "loginentry", @placeholder = "mail id" } })<br> @html.validationmessagefor(o => o.signup.mailid, "", new { @class = "loginvalidation" }) </div> </div> <div class="form-group"> <div class="col-md-10"> @html.editorfor(o => o.signup.password, new { htmlattributes = new { @class = "loginentry", @placeholder = "password", @type = "password" } })<br> @html.validationmessagefor(o => o.signup.password, "", new { @class = "loginvalidation" }) </div> </div> <div class="form-group"> <div class="col-md-10"> @html.editorfor(o => o.signup.confirmpassword, new { htmlattributes = new { @class = "loginentry", @placeholder = "confirm password", @type = "password" } })<br> @html.validationmessagefor(o => o.signup.confirmpassword, "", new { @class = "loginvalidation" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type= "submit" value="signup" id="btn_login" name="command" class="btn btn-default" /> </div> </div> </div> </div> </div> }
Comments
Post a Comment