简体   繁体   English

在单个(已禁用)@Html.EditorFor 上禁用验证

[英]Disable Validation on a single (disabled) @Html.EditorFor

I've read around that the given way of doing this seems to be having different view models (which is a bit overkill imo) for different actions/controllers.我已经读到,给定的执行此操作的方法似乎是针对不同的操作/控制器使用不同的视图模型(在我看来有点矫枉过正)。

I thought我想

@Html.EditorFor(model => model.Ingredient.Name, new { htmlAttributes = new { @class = "form-control", disabled = "disabled", data_val = "false"} })

might work but unfortunately not.可能有效但不幸的是没有。

Does anyone have any ideas?有人有什么想法吗?

You can make use of .ignore to turn of the client validation on one or more elements.您可以使用.ignore来关闭对一个或多个元素的客户端验证。

The nice thing about this is that it is generic an can be applied to multiple elements by just adding the used ignore class.这样做的好处是它是通用的,只需添加使用过的忽略类即可应用于多个元素。

Add this in your jquery code将此添加到您的 jquery 代码中

$.validator.setDefaults({
    ignore: ".ignore"
});

Apply the ignore class on each element you whish to ignore/disable client side validation.在您希望忽略/禁用客户端验证的每个元素上应用忽略类。

@Html.EditorFor(model => model.Ingredient.Name, new { htmlAttributes = new { @class = "form-control ignore", disabled = "disabled", data_val = "false"} })

MVC can be funny when it comes to declaring elements as disabled.当涉及到将元素声明为禁用时,MVC 可能很有趣。 I used readonly instead to get around this issue.我改用 readonly 来解决这个问题。 There just change disabled to readonly as shown below.只是将禁用更改为只读,如下所示。

@Html.EditorFor(model => model.Ingredient.Name, new { htmlAttributes = new { @class = "form-control", readonly= "readonly", data_val = "false"} })

or an alternative is using an input html element或者替代方法是使用输入 html 元素

<input type="text" name="Ingredient.Name" id="Ingredient_Name" readonly="readonly" class="form-control, input-disabled" />

add css class添加 CSS 类

.input-disabled /* CHANGE STYLING OF READONLY FIELDS */
{
    background-color: #EEEEEE;    
}

then add your class to your html然后将您的课程添加到您的html

@Html.EditorFor(model => model.Ingredient.Name, new { htmlAttributes = new { @class = "form-control, input-disabled", readonly= "readonly", data_val = "false"} })

I've created a the following to show you jsfiddle我创建了以下内容来向您展示jsfiddle

All unobtrusive validation (the kind used by ASP.NET MVC) is predicated on finding and parsing data attributes on each object.所有不显眼的验证(ASP.NET MVC 使用的那种)都基于查找和解析每个对象的data属性。 Those carry all the information that the validation engine needs to run and only elements that have data-val="true" are included.这些包含验证引擎运行所需的所有信息,并且仅包含具有data-val="true"的元素。

Any HTML attributes you pass will override any of the default attributes, so the easiest way is to just force data-val="false" and the form element won't be included in the validation engine and none of the other dependent tags will be parsed.您传递的任何 HTML 属性都将覆盖任何默认属性,因此最简单的方法是强制data-val="false"并且表单元素不会包含在验证引擎中,其他任何相关标签都不会解析。

Here's an example from ASP .NET MVC Disable Client Side Validation at Per-Field Level :下面是ASP .NET MVC 在每个字段级别禁用客户端验证的示例:

@Html.TextBoxFor(m => m.BatchId, new { data_val = "false" })

You can disable client side validation in the Razor view before you render field and re-enable.在呈现字段并重新启用之前,您可以在 Razor 视图中禁用客户端验证。 I believe your question has already been answered here我相信你的问题已经在这里得到回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM