I am working with MVC 3 + Razor and client-side JS Model validation isn't working... not sure what is causing problem the problem as I am following all the recommended steps.
public class CompanyProfile
{
[Required]
[StringLength(100, ErrorMessage = "Length can't be more than 100 characters.")]
[Display(Name = "Company name")]
public string CompanyName { get; set; }
[StringLength(100, ErrorMessage = "Length can't be more than 100 characters.")]
[Display(Name = "Company name in URL")]
public string CompanyNameForURL { get; set; }
public Boolean IsActive { get; set; }
public Boolean IsDeleted { get; set; }
}
I download the project from the link, and it works fine for me. Please try create a new project and put this code into it, then see whether it will work.
Regards
Young Yang
Please mark the replies as answers if they help or unmark if not.
Feedback to us
You mentioned, you downloaded and it worked !!! Are you sure that you haven't changed anything and the validation happens via JS and not after POST request?
I am using VS 2010 Professional, which one are you using???
Ofcourse, if I copy everything, it works.... but its an already developed project and copying whole project can be a tedious task and may introduce some error (so proper testing would be required again.)
In my application, Html.BeginForm() is in the Layout file, which is causing the problem. FormContext is null when the child page is processed and hence the required tags aren't rendered.
ankur.nigam
Member
536 Points
141 Posts
JS Validation not working in MVC 3
Apr 17, 2012 01:13 PM|LINK
Hello,
I am working with MVC 3 + Razor and client-side JS Model validation isn't working... not sure what is causing problem the problem as I am following all the recommended steps.
Config in web.Config
<appSettings> <add key="webpages:Version" value="1.0.0.0" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings>Model Class
public class CompanyProfile { [Required] [StringLength(100, ErrorMessage = "Length can't be more than 100 characters.")] [Display(Name = "Company name")] public string CompanyName { get; set; } [StringLength(100, ErrorMessage = "Length can't be more than 100 characters.")] [Display(Name = "Company name in URL")] public string CompanyNameForURL { get; set; } public Boolean IsActive { get; set; } public Boolean IsDeleted { get; set; } }JS files referenced in Master.cshtml
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>Code from CSHTML Page
@Html.TextBoxFor(model => model.CompanyName, new { @class = "xlarge", autocomplete = "off" }) @Html.ValidationMessageFor(model => model.CompanyName, String.Empty, new { @class = "help-inline", @style = "color:red" })Also, I have uploaded the trimmed down sample which can be downloaded from the following link... please suggest.
Download link: http://speedy.sh/gHfxE/WebManagementMvc.zip
(when the page is rendered, click on "WebManagementMvc.zip")
Ankur
My Blogs:
Twitter | DevAstrum
zuperboy90
Participant
977 Points
819 Posts
Re: JS Validation not working in MVC 3
Apr 17, 2012 01:28 PM|LINK
Hello,
When you pass the model to the view, do you initialize it? like
ankur.nigam
Member
536 Points
141 Posts
Re: JS Validation not working in MVC 3
Apr 17, 2012 02:09 PM|LINK
Yes, I did.
Ankur
My Blogs:
Twitter | DevAstrum
Young Yang -...
All-Star
21344 Points
1818 Posts
Microsoft
Re: JS Validation not working in MVC 3
Apr 19, 2012 08:10 AM|LINK
Hi
I download the project from the link, and it works fine for me. Please try create a new project and put this code into it, then see whether it will work.
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store
ankur.nigam
Member
536 Points
141 Posts
Re: JS Validation not working in MVC 3
Apr 20, 2012 12:12 PM|LINK
You mentioned, you downloaded and it worked !!! Are you sure that you haven't changed anything and the validation happens via JS and not after POST request?
I am using VS 2010 Professional, which one are you using???
Ofcourse, if I copy everything, it works.... but its an already developed project and copying whole project can be a tedious task and may introduce some error (so proper testing would be required again.)
Ankur
My Blogs:
Twitter | DevAstrum
zuperboy90
Participant
977 Points
819 Posts
Re: JS Validation not working in MVC 3
Apr 20, 2012 12:19 PM|LINK
Can you inspect the html input element with firebug and see if it has any data-val, data-val-required attributes?
ankur.nigam
Member
536 Points
141 Posts
Re: JS Validation not working in MVC 3
Apr 23, 2012 02:48 PM|LINK
No, when the page is rendered for the first time, none of these attributes are generated
Dunn know what is preventing it from generating it.
Ankur
My Blogs:
Twitter | DevAstrum
gopakumar.r
Participant
959 Points
193 Posts
Re: JS Validation not working in MVC 3
Apr 23, 2012 04:03 PM|LINK
Add a form to your CSHTML code as below
@using (Html.BeginForm()) { <fieldset> @* To turn it on, needs to enable the visibility. *@ <div class="clearfix"> @Html.LabelFor(model => model.CompanyName)@* Company name *@ <div class="input"> @Html.TextBoxFor(model => model.CompanyName, new { @class = "xlarge", autocomplete = "off" }) @* Prevents Autocomplete. *@ <span id="CompanyName_Help_Container" class="help-inline" style="display:none;visibility:hidden;" >Company name as in URL will be: <span id="CompanyName_Help_Container_CompanyName"></span></span> @Html.ValidationMessageFor(model => model.CompanyName, String.Empty, new { @class = "help-inline", @style = "color:red" }) </div> </div> @Html.HiddenFor(model => model.CompanyNameForURL) <div class="actions"> <button type="submit" class="btn primary">Register Company</button> <button type="reset" class="btn">Clear Form</button> </div> </fieldset> }Tell me if it helps!!
Gopakumar
| Please click “Mark as Answer” on the post(s) if it helps |
ankur.nigam
Member
536 Points
141 Posts
Re: JS Validation not working in MVC 3
May 07, 2012 09:22 AM|LINK
I have found out the solution to my situation.
In my application, Html.BeginForm() is in the Layout file, which is causing the problem. FormContext is null when the child page is processed and hence the required tags aren't rendered.
Check out the following link for more details: http://forums.asp.net/t/1645065.aspx/1#
Ankur
My Blogs:
Twitter | DevAstrum