asp.net-mvc 在 Razor 视图中启用客户端验证 (ASP MVC 3)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3695186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:29:02  来源:igfitidea点击:

Enable client validation in Razor views (ASP MVC 3)

asp.net-mvcrazorasp.net-mvc-3

提问by Martin

I try to add client side validation using this line of code:

我尝试使用这行代码添加客户端验证:

@Html.EnableClientValidation()

@Html.EnableClientValidation()

But I keep getting this error message:

但我不断收到此错误消息:

Compiler Error Message: CS1502: The best overloaded method match for 'Microsoft.WebPages.WebPageUltimateBase.Write(Microsoft.WebPages.Helpers.HelperResult)' has some invalid arguments

编译器错误消息:CS1502:“Microsoft.WebPages.WebPageUltimateBase.Write(Microsoft.WebPages.Helpers.HelperResult)”的最佳重载方法匹配有一些无效参数

Is this working for anyone else, or is it another approach for this in ASP MVC 3?

这对其他人有用吗,还是 ASP MVC 3 中的另一种方法?

回答by Buildstarted

You can, instead, use the following in place of the expected line of code.

相反,您可以使用以下内容代替预期的代码行。

@(ViewContext.ClientValidationEnabled = true)

Probably an oversight in the extension methods for htmlhelper.

可能是 htmlhelper 扩展方法的疏忽。

Actually, you can use the HtmlHelper method by doing the following

实际上,您可以通过执行以下操作来使用 HtmlHelper 方法

@{ Html.EnableClientValidation(); }

回答by bearing09

Hey, in ASP.NET MVC3, there's no need to add Html.EnableClientValidation() in view page, instead, just enable the clientValidation in the webconfig file as below:

嘿,在 ASP.NET MVC3 中,不需要在视图页面中添加 Html.EnableClientValidation() ,而是只需在 webconfig 文件中启用 clientValidation ,如下所示:

<appSettings>
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

回答by marcelo

this tag

这个标签

     @{ Html.EnableClientValidation(false); }

must come before the

必须在

  @using (Html.BeginForm())

回答by Simon

Are you using the html <form>element on your page instead of Html.BeginFormto create your html FORM.

您是否<form>在页面上使用 html元素而不是Html.BeginForm创建 html 表单。

I had this exact same problem and worked out it was because the i was not using Html.BeginFormto create my FORM resulting in the required input attributes data-val-required="The Email field is required." data-val="true" class="input-validation-errorand the place holder for the validation was not being injected into the page even though i had the @Html.ValidationMessageFor(m => m.User.Role)inserted on my view page.

我遇到了完全相同的问题并解决了它是因为我没有Html.BeginForm用来创建我的 FORM 导致所需的输入属性data-val-required="The Email field is required." data-val="true" class="input-validation-error和验证的占位符没有被注入到页面中,即使我@Html.ValidationMessageFor(m => m.User.Role)在我的视图中插入了页。

回答by MaurGi

In my case, I was not using EditorFor, but TextBoxFor!

就我而言,我没有使用 EditorFor,而是使用 TextBoxFor!

Make sure you use:

确保您使用:

             <td>@Html.EditorFor(m => m.Email)</td>