html - Validation is not working mvc -
i can`t find out wrong. trying no allow setting null values field, when user tries it, must show message "*", , wait him set values, doesnot work , null values sent action. model :
public class companymaininfomodel { public int companyid { get; set; } [required(errormessage = "*")] [display(name = "company_name", resourcetype = typeof(localization))] public string companyname { get; set; } [required(errormessage = "*")] [display(name = "company_address", resourcetype = typeof(localization))] public string companyaddress { get; set; } [required(errormessage = "*")] [display(name = "company_director", resourcetype = typeof(localization))] public string companydirector { get; set; } [required(errormessage = "*")] [display(name = "company_phone", resourcetype = typeof(localization))] public string companytelephonenumber { get; set; } }
markup :
@model btghrm.models.companymaininfomodel @{ layout = "~/views/shared/_employeemain.cshtml"; } <head> <script src="~/scripts/jquery-1.10.2.js"></script> <script src="~/scripts/jquery-ui.js"></script> <script src="~/scripts/jquery.validate.unobtrusive.js"></script> <script src="~/scripts/jquery.validate.js"></script> </head> <body> <span class="content_h4">@resources.localization.company_info</span> <br /> <br /> <div id="formcontainer"> @html.partial("partial/_companyinfo", model) </div> </body>
partial markup:
@model btghrm.models.companymaininfomodel @using (html.beginform("companyinfo", "administration")) { <table> <tr> <td>@html.labelfor(m => m.companyname)</td> <td>@html.textboxfor(m => m.companyname)</td> <td>@html.validationmessagefor(m => m.companyname, "" , new { @class="text-danger"})</td> </tr> <tr> <td>@html.labelfor(m => m.companyaddress)</td> <td>@html.textboxfor(m => m.companyaddress)</td> <td>@html.validationmessagefor(m => m.companyaddress, "", new { @class = "text-danger" })</td> </tr> <tr> <td>@html.labelfor(m => m.companydirector)</td> <td>@html.textboxfor(m => m.companydirector)</td> <td>@html.validationmessagefor(m => m.companydirector, "", new { @class = "text-danger" })</td> </tr> <tr> <td>@html.labelfor(m => m.companytelephonenumber)</td> <td>@html.textboxfor(m => m.companytelephonenumber)</td> <td>@html.validationmessagefor(m => m.companytelephonenumber, "", new { @class = "text-danger" })</td> </tr> </table> <input style="width:78px" type="submit" value=@resources.localization.save /> }
and despite [required] still allows me set null values field. wrong code?
you have have unobtrusive scripts enabled in web config
<add key="unobtrusivejavascriptenabled" value="true"/>
then need add
- jquery
- jquery.validate.js
- jquery.validate.unobtrusive.js
Comments
Post a Comment