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

标签 asp.net-mvc razor asp.net-mvc-5 readonly editorformodel

我在 MVC5 Code-First 应用程序的大多数 View 中显示了几个字段:[created_date][created_by][modified_date] [modified_by]。对于用户,我想将这些也包含在我的 Edit() View 中,但与我的其他字段不同,我不希望它们可编辑(通常创建/由和修改/由数据不应该可编辑)。

这就是我当前在 Edit() View 上声明字段的方式:

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

任何人都可以提供一种方法来使这些字段只读吗?

我尝试了 DisplayFor() 就像在我的 Details View 上一样,但是当我去保存记录时,我的 EditorFor() 中的值> [created_date] 从“2015-02-18”更改为“0001-01-01”和 created_by 从我的系统用户名更改为之前设置为 null 的验证消息“Thecreated_by 字段是必需的”。我相信这与我的字段模型注释有关,但我不明白为什么 DisplayFor() 没有像 EditorFor() 那样通过我的 Controller 传递值 是……吗?

模型注释:

    [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),因此一如既往,使用 View 模型是最好的方法( View 模型不会包含验证属性) 'readonly' 属性),然后在发布时获取原始数据模型并将相关 View 模型属性映射到数据模型,然后保存。

关于asp.net-mvc - 在 MVC View 上将 EditorFor() 值显示为只读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28593172/

相关文章:

ASP.NET 路由目录请求

html - 如何在文本和冒号之间的 HTML 标签内为不同长度的文本添加空格以实现完美对齐?

c# - JSON调用C#类方法

html - Razor View 中的垂直对齐

c# - 在 Entity framework6 Repository 模式中限制用户只能访问他/她的实体

asp.net-mvc - 以表单上传图像并在 MVC 4 上显示

asp.net-mvc - 在MVC5中使用OAuth时,无限循环返回到身份验证页面

asp.net-mvc - ASP.NET MVC View 引擎比较

c# - 具有继承实体的 MVC 5 脚手架代码生成导致 System.Core 已导入

c# - SQL 存储过程未返回小数位