在单个(禁用)@ Html.EditorFor上禁用验证 [英] Disable Validation on a single (disabled) @Html.EditorFor

查看:151
本文介绍了在单个(禁用)@ Html.EditorFor上禁用验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过了,给定的执行方法似乎是针对不同的动作/控制器使用不同的视图模型(这是个过大的imo).

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.

我以为

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

可能会工作,但不幸的是不会.

might work but unfortunately not.

有人有什么想法吗?

推荐答案

在将元素声明为禁用时,MVC可能很有趣.我改用readonly来解决此问题.只是将禁用更改为只读,如下所示.

MVC can be funny when it comes to declaring elements as disabled. I used readonly instead to get around this issue. 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"} })

或替代方法是使用输入的html元素

or an alternative is using an input html element

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

添加CSS类

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

然后将您的课程添加到html

then add your class to your html

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

我创建了以下内容,向您展示 jsfiddle

I've created a the following to show you jsfiddle

这篇关于在单个(禁用)@ Html.EditorFor上禁用验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆