【发布时间】:2012-02-04 09:46:48
【问题描述】:
如何使用 Razor 视图引擎在 ASP.NET MVC3 中创建只读文本框?
是否有可用的 HTMLHelper 方法来执行此操作?
类似以下内容?
@Html.ReadOnlyTextBoxFor(m => m.userCode)
【问题讨论】:
标签: c# asp.net-mvc-3 razor html-helper readonly-attribute
如何使用 Razor 视图引擎在 ASP.NET MVC3 中创建只读文本框?
是否有可用的 HTMLHelper 方法来执行此操作?
类似以下内容?
@Html.ReadOnlyTextBoxFor(m => m.userCode)
【问题讨论】:
标签: c# asp.net-mvc-3 razor html-helper readonly-attribute
@Html.TextBoxFor(m => m.userCode, new { @readonly="readonly" })
欢迎您为此创建一个 HTML Helper,但这只是一个 HTML 属性,就像其他任何属性一样。您会为具有其他属性的文本框制作 HTML 助手吗?
【讨论】:
readonly 属性的@ 前缀。查看我的编辑。
@。您通常只会看到与 HTML 属性匹配的关键字(如只读、类等)
@ 即可使用匹配 C# 关键字的属性。
@Html.TextBoxFor(m => m.userCode, new { @readonly="readonly", @class="form-control" })
更新: 现在,将 HTML 属性添加到默认编辑器模板非常简单。它不是这样做:
@Html.TextBoxFor(m => m.userCode, new { @readonly="readonly" })
你可以这样做:
@Html.EditorFor(m => m.userCode, new { htmlAttributes = new { @readonly="readonly" } })
优点:您不必致电.TextBoxFor 等获取模板。只需致电.EditorFor。
虽然@Shark 的解决方案工作正常,而且简单实用,但我的解决方案(我一直使用的)是这样的:创建一个可以处理readonly 属性的editor-template:
~/Views/Shared/ 中创建一个名为EditorTemplates 的文件夹
PartialView,命名为String.cshtml
用此代码填写String.cshtml:
@if(ViewData.ModelMetadata.IsReadOnly) {
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
new { @class = "text-box single-line readonly", @readonly = "readonly", disabled = "disabled" })
} else {
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
new { @class = "text-box single-line" })
}
在模型类中,将[ReadOnly(true)] 属性放在您想成为readonly 的属性上。
例如,
public class Model {
// [your-annotations-here]
public string EditablePropertyExample { get; set; }
// [your-annotations-here]
[ReadOnly(true)]
public string ReadOnlyPropertyExample { get; set; }
}
现在您可以简单地使用 Razor 的默认语法:
@Html.EditorFor(m => m.EditablePropertyExample)
@Html.EditorFor(m => m.ReadOnlyPropertyExample)
第一个渲染一个普通的text-box,像这样:
<input class="text-box single-line" id="field-id" name="field-name" />
第二个会渲染到;
<input readonly="readonly" disabled="disabled" class="text-box single-line readonly" id="field-id" name="field-name" />
您可以将此解决方案用于任何类型的数据(DateTime、DateTimeOffset、DataType.Text、DataType.MultilineText 等)。只需创建一个editor-template。
【讨论】:
TextBoxFor 的解决方案是可以的,但是如果你不想看到像 EditBox 这样时髦的字段(这可能会让用户有点困惑)涉及如下更改:
修改前的剃刀代码
<div class="editor-field">
@Html.EditorFor(model => model.Text)
@Html.ValidationMessageFor(model => model.Text)
</div>
修改后
<!-- New div display-field (after div editor-label) -->
<div class="display-field">
@Html.DisplayFor(model => model.Text)
</div>
<div class="editor-field">
<!-- change to HiddenFor in existing div editor-field -->
@Html.HiddenFor(model => model.Text)
@Html.ValidationMessageFor(model => model.Text)
</div>
通常,此解决方案会阻止编辑字段,但会显示其价值。 无需进行代码隐藏修改。
【讨论】:
感谢@Bronek 和@Shimmy 之前的回答:
这就像我在 ASP.NET Core 中做过同样的事情:
<input asp-for="DisabledField" disabled="disabled" />
<input asp-for="DisabledField" class="hidden" />
第一个输入是只读的,第二个将值传递给控制器并隐藏。我希望它对使用 ASP.NET Core 的人有用。
【讨论】:
@Html.TextBox("Receivers", Model, new { @class = "form-control", style = "width: 300px", @readonly = "readonly" })
【讨论】:
@Html.TextBoxFor(model => model.IsActive, new { readonly= "readonly" })
这对于文本框来说很好。但是,如果您尝试对 checkbox 执行相同操作,那么如果您正在使用它,请尝试使用它:
@Html.CheckBoxFor(model => model.IsActive, new { onclick = "return false" })
但不要使用disable,因为禁用总是将默认值false 发送到服务器——它处于选中或未选中状态。而readonly 不适用于复选框和radio button。 readonly 仅适用于 text 字段。
【讨论】:
您可以使用以下代码将 TextBox 创建为只读。
方法一
@Html.TextBoxFor(model => model.Fields[i].TheField, new { @readonly = true })
方法二
@Html.TextBoxFor(model => model.Fields[i].TheField, new { htmlAttributes = new {disabled = "disabled"}})
【讨论】: