简体   繁体   English

客户端验证无法通过数据注释进行; 服务器端

[英]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.

相关问题 从客户端正则表达式实现服务器端正则表达式验证 - implementing server-side regex validation from client-side regex 自定义DataAnnotations ValueAttribute在客户端和服务器端验证中提供了不同的错误消息 - Custom DataAnnotations ValueAttribute suppling different error message on client-side and server-side validation 输入验证是针对企业应用程序的客户端还是服务器端? - Should input validation be client-side or server-side for Enterprise applications? ASP.NET客户端与服务器端验证 - ASP.NET Client-side vs Server-side Validation 如何在 ASP.NET Core 3.1 MVC 中进行RequiredIf 客户端和服务器端验证? - How to make RequiredIf Client-side and server-side validation in ASP.NET Core 3.1 MVC? .NET MVC 解决方案与 DAL 项目:放置自定义验证属性(服务器端和客户端)的位置 - .NET MVC solution with DAL project: where to put custom validation attributes (server-side and client-side) 用于客户端和服务器端验证的 ASP.NET Core 重用代码 - ASP.NET Core Reuse Code for Client-Side and Server-Side Validation ASP.NET:客户端验证,服务器端操作 - ASP.NET: Client-side validation, server-side action 将数据发布到web方法或将客户端数据转储到服务器控件然后获取数据服务器端是否更快? - Is it faster to POST data to a webmethod or dump client-side data to a server control and then get the data server-side? RadTreeView上的客户端事件和服务器端事件冲突 - Client-side event and Server-side event conflict on RadTreeView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM