[英]Client-side validation not working from data annotations; server-side does
I'm having trouble getting client validation to work on a web form. 我无法让客户验证在Web表单上工作。 I'm including .js files and have the relevant lines in web.config, but data validation doesn't get put into the HTML on the client side.
我包括.js文件,并在web.config中包含相关行,但客户端没有将数据验证放入HTML中。
My layout, which is being applied to the page, looks like 正在应用到页面的我的布局看起来像
<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript" ></ script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript" ></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript" ></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript" ></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcValidation.js")" type="text/javascript"></script>
</head>
My web.config file has the following: 我的web.config文件具有以下内容:
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
<add key="webpages:Enabled" value="false" />
</appSettings>
My page's viewmodel has proper annotations and is being validated properly on the server-side 我页面的viewmodel具有正确的批注,并且已在服务器端正确验证
[DisplayName("Url")]
[DataType(DataType.Url, ErrorMessage = "Invalid Url")]
[StringLength(30)]
[Required]
public string url { get; set; }
[DisplayName("Email")]
[DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email Address")]
[Required]
public string email { get; set; }
Finally, I am including the following HTML in my page: 最后,我在页面中包含以下HTML:
<div class="conLine">
@Html.LabelFor(m=> m.url): @Html.TextBoxFor(m => m.url)
@Html.ValidationMessageFor(m => m.url)
</div>
<div class="conLine">
@Html.LabelFor(m=> m.email): @Html.TextBoxFor(m => m.email)
@Html.ValidationMessageFor(m => m.email)
</div>
The HTML output by the page at this section looks like: 该部分页面的HTML输出如下所示:
<div class="conLine">
<label for="url">Url</label>
:
<input class="input-validation-error" id="url" name="url" type="text" value=""/>
<span class="field-validation-error">The Url field is required.</span>
</div>
I'm about at my wit's end with this. 我快要结束了。 Any help would be greatly appreciated!
任何帮助将不胜感激!
You should wrap these code in Html.BeginForm 您应该将这些代码包装在Html.BeginForm中
@using (Html.BeginForm()) {
<div class="conLine">
@Html.LabelFor(m=> m.url): @Html.TextBoxFor(m => m.url)
@Html.ValidationMessageFor(m => m.url)
</div>
<div class="conLine">
@Html.LabelFor(m=> m.email): @Html.TextBoxFor(m => m.email)
@Html.ValidationMessageFor(m => m.email)
</div>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.