如何使用 Razor 视图引擎在 ASP.NET MVC3 中创建只读文本框?
是否有可用的 HTMLHelper 方法来做到这一点?
像下面这样的东西?
@Html.ReadOnlyTextBoxFor(m => m.userCode)
如何使用 Razor 视图引擎在 ASP.NET MVC3 中创建只读文本框?
是否有可用的 HTMLHelper 方法来做到这一点?
像下面这样的东西?
@Html.ReadOnlyTextBoxFor(m => m.userCode)
@Html.TextBoxFor(m => m.userCode, new { @readonly="readonly" })
欢迎您为此创建一个 HTML Helper,但这只是一个 HTML 属性,就像任何其他属性一样。您会为具有其他属性的文本框制作 HTML 助手吗?
更新: 现在将 HTML 属性添加到默认编辑器模板非常简单。它不需要这样做:
@Html.TextBoxFor(m => m.userCode, new { @readonly="readonly" })
你可以这样做:
@Html.EditorFor(m => m.userCode, new { htmlAttributes = new { @readonly="readonly" } })
好处:您不必.TextBoxFor
为模板调用等。只要打电话.EditorFor
。
虽然@Shark 的解决方案工作正常,而且它简单实用,但我的解决方案(我一直使用的)是这个:创建一个editor-template
可以处理readonly
属性的解决方案:
EditorTemplates
夹~/Views/Shared/
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
,因为 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"}})