在MVC视图上将EditorFor()值显示为只读吗? [英] Display EditorFor() value as Read-Only on MVC View?

查看:75
本文介绍了在MVC视图上将EditorFor()值显示为只读吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我目前在Edit()视图上声明的字段的方式:

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

    <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的方法吗?

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

我尝试在Details视图上尝试DisplayFor(),但是当我保存记录时,EditorFor()EditorFor()中的值从" 2015-02-18 >"更改为" 0001-01-01 ",并且created_by从我的系统用户名更改为先前设置为null的内容,并显示验证消息" created_by字段是必需的 ."我相信这与字段的模型注释有关,但是我不明白为什么DisplayFor()不能像EditorFor()那样通过控制器传递值...?

I tried DisplayFor() like on my Details View, 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_by changes from my system username as previously set to null with 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...?

模型注释:

    [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()不会生成回发的控件.除了显示文字之外,您还需要包括一个隐藏的输入,例如

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

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

请注意,添加@Html.ValidationMessageFor()

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

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.

这篇关于在MVC视图上将EditorFor()值显示为只读吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆