在 MVC 视图上将 EditorFor() 值显示为只读?
Display EditorFor() value as Read-Only on MVC View?
我在 MVC5 Code-First 应用程序的大部分视图中显示了几个字段:[created_date]
、[created_by]
、[modified_date]
和 [modified_by]
。对于用户,我也想在我的 Edit()
视图中包含这些,但与我的其他字段不同,我不希望它们可编辑(通常 created/by 和 modified/by 数据不应该可编辑)。
这就是我目前在 Edit()
视图中声明字段的方式:
<div class="form-group">
@*@Html.LabelFor(model => model.created_date, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">Created Date:</span>
<div class="col-md-10">
@Html.DisplayFor(model => model.created_date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.created_date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@*@Html.LabelFor(model => model.created_by, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">By:</span>
<div class="col-md-10">
@Html.DisplayFor(model => model.created_by, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.created_by, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@*@Html.LabelFor(model => model.modified_date, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">Modified Date:</span>
<div class="col-md-10">
@Html.DisplayFor(model => model.modified_date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.modified_date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@*@Html.LabelFor(model => model.modified_by, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">By:</span>
<div class="col-md-10">
@Html.DisplayFor(model => model.modified_by, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.modified_by, "", new { @class = "text-danger" })
</div>
</div>
谁能提供制作这些字段的方法read-only
?
我在 Details
视图中尝试了 DisplayFor()
,但是当我去保存记录时,EditorFor()
中 [created_date]
的值从“2015-02-18" 到 "0001-01-01" 并且 created_by
从我之前设置的系统用户名更改为 null
,验证消息为“需要 created_by 字段。”我相信这与我的字段模型注释有关,但我不明白为什么 DisplayFor()
没有像 EditorFor()
那样通过我的控制器传递值......?
模型注释:
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime created_date { get; set; }
[Required]
public string created_by { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? modified_date { get; set; }
public string modified_by { get; set; }
DisplayFor()
不生成回发控件。除了显示文本之外,您还需要包括一个隐藏的输入,例如
<div class="form-group">
<span class="control-label col-md-2">@Html.DisplayNameFor(m => created_by)</span>
<div class="col-md-10">
@Html.DisplayFor(model => model.created_by
@Html.HiddenFor(m => created_by)
</div>
</div>
另一种选择是将文本框设置为只读
<div class="form-group">
@Html.LabelFor(model => model.created_by, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.created_by, new { @class = "form-control", @readonly = "readonly" })
</div>
</div>
注意添加 @Html.ValidationMessageFor()
也没有意义
旁注:没有什么可以阻止用户更改隐藏输入的值(例如使用 FireBug),因此一如既往,使用视图模型是最好的方法(视图模型会不包括 'readonly' 属性上的验证属性),然后在发布时获取原始数据模型并在保存之前将相关视图模型属性映射到数据模型。
我在 MVC5 Code-First 应用程序的大部分视图中显示了几个字段:[created_date]
、[created_by]
、[modified_date]
和 [modified_by]
。对于用户,我也想在我的 Edit()
视图中包含这些,但与我的其他字段不同,我不希望它们可编辑(通常 created/by 和 modified/by 数据不应该可编辑)。
这就是我目前在 Edit()
视图中声明字段的方式:
<div class="form-group">
@*@Html.LabelFor(model => model.created_date, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">Created Date:</span>
<div class="col-md-10">
@Html.DisplayFor(model => model.created_date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.created_date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@*@Html.LabelFor(model => model.created_by, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">By:</span>
<div class="col-md-10">
@Html.DisplayFor(model => model.created_by, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.created_by, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@*@Html.LabelFor(model => model.modified_date, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">Modified Date:</span>
<div class="col-md-10">
@Html.DisplayFor(model => model.modified_date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.modified_date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@*@Html.LabelFor(model => model.modified_by, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">By:</span>
<div class="col-md-10">
@Html.DisplayFor(model => model.modified_by, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.modified_by, "", new { @class = "text-danger" })
</div>
</div>
谁能提供制作这些字段的方法read-only
?
我在 Details
视图中尝试了 DisplayFor()
,但是当我去保存记录时,EditorFor()
中 [created_date]
的值从“2015-02-18" 到 "0001-01-01" 并且 created_by
从我之前设置的系统用户名更改为 null
,验证消息为“需要 created_by 字段。”我相信这与我的字段模型注释有关,但我不明白为什么 DisplayFor()
没有像 EditorFor()
那样通过我的控制器传递值......?
模型注释:
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime created_date { get; set; }
[Required]
public string created_by { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? modified_date { get; set; }
public string modified_by { get; set; }
DisplayFor()
不生成回发控件。除了显示文本之外,您还需要包括一个隐藏的输入,例如
<div class="form-group">
<span class="control-label col-md-2">@Html.DisplayNameFor(m => created_by)</span>
<div class="col-md-10">
@Html.DisplayFor(model => model.created_by
@Html.HiddenFor(m => created_by)
</div>
</div>
另一种选择是将文本框设置为只读
<div class="form-group">
@Html.LabelFor(model => model.created_by, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.created_by, new { @class = "form-control", @readonly = "readonly" })
</div>
</div>
注意添加 @Html.ValidationMessageFor()
旁注:没有什么可以阻止用户更改隐藏输入的值(例如使用 FireBug),因此一如既往,使用视图模型是最好的方法(视图模型会不包括 'readonly' 属性上的验证属性),然后在发布时获取原始数据模型并在保存之前将相关视图模型属性映射到数据模型。