asp.net-mvc 在 MVC 视图上将 EditorFor() 值显示为只读?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28593172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:24:08  来源:igfitidea点击:

Display EditorFor() value as Read-Only on MVC View?

asp.net-mvcrazorasp.net-mvc-5readonlyeditorformodel

提问by Analytic Lunatic

I have several fields I display on most of my views in my MVC5 Code-First app: [created_date], [created_by], [modified_date], and [modified_by]. For the user, I would like to include these also on my Edit()view, but unlike my other fields I do not wish to have them editable (commonly created/by and modified/by data should NOT be editable).

我有几个字段我在MVC5代码优先应用程序的大多数我的意见显示:[created_date][created_by][modified_date],和[modified_by]。对于用户,我想将这些也包括在我的Edit()视图中,但与我的其他字段不同,我不希望它们可编辑(通常创建/由和修改/由数据不应编辑)。

This is how I have the fields declared on my Edit()view currently:

这就是我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>

Can anyone offer a way to make these fields read-only?

任何人都可以提供一种制作这些字段的方法read-only吗?

I tried DisplayFor()like on my DetailsView, but when I go to save the record, the value in my EditorFor()for [created_date]changes from "2015-02-18" to "0001-01-01" and created_bychanges from my system username as previously set to nullwith the validation message "The created_by field is required." I believe this has something to do with my model annotations for the fields, but I don't see why the DisplayFor()is not passing the value through my controller like how the EditorFor()does...?

我想DisplayFor()喜欢我的Details查看,但是当我去保存记录,该值在我EditorFor()[created_date]从“改变2015年2月18日”为“ 0001-01-01”,并created_by改变从我的系统用户名作为以前被设置为null与验证消息“ created_by 字段是必需的。” 我相信这与我对字段的模型注释有关,但我不明白为什么它不像……DisplayFor()那样通过我的控制器传递值EditorFor()

Model Annotations:

模型注释

    [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; }

回答by

DisplayFor()does not generate a control that posts back. You need to include a hidden input in addition to the display text, for example

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>

Another option is to make the textbox readonly

另一种选择是使文本框只读

<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>

Note also there is no point adding @Html.ValidationMessageFor()

还要注意添加没有意义 @Html.ValidationMessageFor()

Side note: There is nothing to stop a user changing the values of a hidden input (using FireBug for example), so as always, using a view model is the best approach (the view model would not include validation attributes on the 'readonly' properties) and then when posting, get the original data model and map the relevant view model properties to the data model before saving.

旁注:没有什么可以阻止用户更改隐藏输入的值(例如使用 FireBug),因此一如既往,使用视图模型是最好的方法(视图模型不会在“只读”上包含验证属性) properties) 然后在发帖时,获取原始数据模型并将相关的视图模型属性映射到数据模型,然后保存。