c# - validate perticular form controls on respective action -


in mvc web application, have 2 html.beginform() same controller , different action on single view. when click button of first html.beginform(), shows validation message both form want single form. @ code below.

  //first html.beginform() @using (html.beginform("addfirst", "student", formmethod.post, new  { id = "form1", role = "form"  })) {  <tr> <td> @html.labelfor(model => model.mobileno) @html.editorfor(model => model.mobileno, new { htmlattributes = new { id = "textmobile", @class = "form-control", @maxlength = "10", @placeholder = "mobile no" } }) @html.validationmessagefor(model => model.mobileno, "", new { @class = "text-danger" }) </td> <td>     <input type="submit" name="submit" value="first button" class="btn btn-default" /> </td>  </tr>   }     //second html.beginform() @using (html.beginform("addsecond", "student", formmethod.post, new  {     id = "form2", role = "form"  }))  {    <tr>   <td> @html.labelfor(model => model.mobileno) @html.editorfor(model => model.mobileno, new { htmlattributes = new { id = "textmobile2", @class = "form-control", @maxlength = "10", @placeholder = "mobile no" } }) @html.validationmessagefor(model => model.mobileno, "", new { @class = "text-danger" }) </td> <td>     <input type="submit" name="submit" value="second button" class="btn btn-default" />  </td>  </tr>   } 

in above case, when click on first button shows validation message both mobile number, want show validation message 1 mobile number(same second button). how validate single text box of particular form? have taken different action purpose. in controller, have 2 actions.

[httppost]     public actionresult addfirst(string submit, user u)     {         agree = new agree(u);         if (modelstate.isvalid)         {     //some logic             return view("thisview", agree);         }         else         {             return view("thisview", agree);         }      }   [httppost]     public actionresult addsecond(string submit, user u)     {         agree = new agree(u);         if (modelstate.isvalid)         {     //some logic             return view("thisview", agree);         }         else         {             return view("thisview", agree);         }          } 

and in model, have

    [displayname("mobile number")]     [required(errormessage = "* please enter mobile number")]     public string mobileno { get; set; } 

you can creating 3 model class

public class addfirstmobileviewmodel {     [displayname("mobile number")]     [required(errormessage = "* please enter mobile number")]     public string mobileno { get; set; } }  public class addsecondmobileviewmodel {     [displayname("mobile number")]     [required(errormessage = "* please enter mobile number")]     public string mobileno { get; set; } }  public class mobileviewmodel {     public addfirstmobile firstmobilenumber{get;set;}      public addsecondmobile secondmobilenumber{get;set;} } 

in view update code like

@model appname.models.mobileviewmodel  @using (html.beginform("addfirst", "student", formmethod.post, new  { id = "form1", role = "form"  })) {     <tr>         <td>             @html.labelfor(model => model.firstmobilenumber.mobileno)             @html.editorfor(model => model.firstmobilenumber.mobileno, new { htmlattributes = new { id = "textmobile", @class = "form-control", @maxlength = "10", @placeholder = "mobile no" } })             @html.validationmessagefor(model => model.firstmobilenumber.mobileno, "", new { @class = "text-danger" })         </td>         <td>             <input type="submit" name="submit" value="first button" class="btn btn-default" />         </td>     </tr>   }  //second html.beginform() @using (html.beginform("addsecond", "student", formmethod.post, new  {     id = "form2", role = "form"  }))  {     <tr>         <td>             @html.labelfor(model => model.secondmobilenumber.mobileno)             @html.editorfor(model => model.secondmobilenumber.mobileno, new { htmlattributes = new { id = "textmobile2", @class = "form-control", @maxlength = "10", @placeholder = "mobile no" } })             @html.validationmessagefor(model => model.secondmobilenumber.mobileno, "", new { @class = "text-danger" })         </td>         <td>             <input type="submit" name="submit" value="second button" class="btn btn-default" />         </td>     </tr>   } 

after on clicking submit button of 1 form shows validation thr respective form , save data db
hope helps.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -