Ana*_*tic 5 asp.net-mvc readonly editorformodel razor asp.net-mvc-5
我有几个领域我大多数的我的意见显示在我的MVC5代码的第一APP: ,[created_date],[created_by],[modified_date]和[modified_by].对于用户,我想将这些也包括在我的Edit()视图中,但与我的其他字段不同,我不希望它们可编辑(通常创建/通过和修改/数据不应该是可编辑的).
这就是我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>
Run Code Online (Sandbox Code Playgroud)
谁能提供制作这些领域的方法read-only?
我想DisplayFor()喜欢我的Details查看,但是当我去保存记录,该值在我EditorFor()的[created_date]变化,从" 2015年2月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; }
Run Code Online (Sandbox Code Playgroud)
小智 37
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>
Run Code Online (Sandbox Code Playgroud)
另一种选择是使文本框只读
<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>
Run Code Online (Sandbox Code Playgroud)
请注意,没有必要添加 @Html.ValidationMessageFor()
旁注:没有什么可以阻止用户更改隐藏输入的值(例如使用FireBug),因此一如既往,使用视图模型是最好的方法(视图模型不会在'readonly'上包含验证属性发布后,获取原始数据模型并在保存之前将相关视图模型属性映射到数据模型.
| 归档时间: |
|
| 查看次数: |
31152 次 |
| 最近记录: |