ReadOnly(true)是否与Html.EditorForModel一起使用?

时间:2022-09-11 20:50:30

Consider the following setup:

请考虑以下设置:

Model:

模型:

public class Product
{
    [ReadOnly(true)]
    public int ProductID
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }
}

View:

视图:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<MvcApplication4.Models.Product>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%= Html.EditorForModel() %>
</asp:Content>

Controller:

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new Product
            {
                ProductID = 1,
                Name = "Banana"
            });
    }
}

There result is this: ReadOnly(true)是否与Html.EditorForModel一起使用?

结果如下:

I was expecting that the ProductID property was not going to be editable via the ReadOnly(true) attribute. Is this supported? If not is there any way to hint ASP.NET MVC that some properties of my model are read-only? I would not like to just hide ProductID via [ScaffoldColumn(false)].

我期望通过ReadOnly(true)属性不能编辑ProductID属性。这支持吗?如果没有,有没有办法提示ASP.NET MVC我的模型的某些属性是只读的?我不想通过[ScaffoldColumn(false)]隐藏ProductID。

3 个解决方案

#1


9  

The ReadOnly and Required attributes will be consumed by the metadata provider but won't be used. If you want to get rid of the input with EditorForModel you'll need a custom template, or [ScaffoldColumn(false)].

ReadOnly和Required属性将由元数据提供程序使用,但不会被使用。如果你想用EditorForModel去掉输入,你需要一个自定义模板,或者[ScaffoldColumn(false)]。

For custom template ~/Views/Home/EditorTemplates/Product.ascx:

对于自定义模板〜/ Views / Home / EditorTemplates / Product.ascx:

<%@ Control Language="C#" Inherits="ViewUserControl<Product>" %>

<%: Html.LabelFor(x => x.ProductID) %>
<%: Html.TextBoxFor(x => x.ProductID, new { @readonly = "readonly" }) %>

<%: Html.LabelFor(x => x.Name) %>
<%: Html.TextBoxFor(x => x.Name) %>

Also note that the default model binder won't copy a value into a property with [ReadOnly(false)]. This attribute won't influence the UI rendered by the default templates.

另请注意,默认模型绑定程序不会使用[ReadOnly(false)]将值复制到属性中。此属性不会影响默认模板呈现的UI。

#2


11  

I solved this problem by adding a UIHintAttribute to the property on my class of "ReadOnly".

我通过在我的“ReadOnly”类的属性中添加UIHintAttribute来解决这个问题。

[UIHint("ReadOnly")]
public int ClassID { get; set; }

Then I simply added a ~\Views\Shared\EditorTemplates\ReadOnly.ascx file to my project with this in it:

然后我简单地在其中添加了一个〜\ Views \ Shared \ EditorTemplates \ ReadOnly.ascx文件到我的项目中:

<%= Model %>

A really simple way to add custom templates, you could include formatting or whatever.

添加自定义模板的一种非常简单的方法,您可以包括格式或其他。

#3


2  

<%@ Control Language="C#" Inherits="ViewUserControl<Product>" %>

<%: Html.LabelFor(x => x.ProductID) %>
<%: Html.TextBoxFor(x => x.ProductID, new { @readonly = true }) %>

<%: Html.LabelFor(x => x.Name) %>
<%: Html.TextBoxFor(x => x.Name) %>

#1


9  

The ReadOnly and Required attributes will be consumed by the metadata provider but won't be used. If you want to get rid of the input with EditorForModel you'll need a custom template, or [ScaffoldColumn(false)].

ReadOnly和Required属性将由元数据提供程序使用,但不会被使用。如果你想用EditorForModel去掉输入,你需要一个自定义模板,或者[ScaffoldColumn(false)]。

For custom template ~/Views/Home/EditorTemplates/Product.ascx:

对于自定义模板〜/ Views / Home / EditorTemplates / Product.ascx:

<%@ Control Language="C#" Inherits="ViewUserControl<Product>" %>

<%: Html.LabelFor(x => x.ProductID) %>
<%: Html.TextBoxFor(x => x.ProductID, new { @readonly = "readonly" }) %>

<%: Html.LabelFor(x => x.Name) %>
<%: Html.TextBoxFor(x => x.Name) %>

Also note that the default model binder won't copy a value into a property with [ReadOnly(false)]. This attribute won't influence the UI rendered by the default templates.

另请注意,默认模型绑定程序不会使用[ReadOnly(false)]将值复制到属性中。此属性不会影响默认模板呈现的UI。

#2


11  

I solved this problem by adding a UIHintAttribute to the property on my class of "ReadOnly".

我通过在我的“ReadOnly”类的属性中添加UIHintAttribute来解决这个问题。

[UIHint("ReadOnly")]
public int ClassID { get; set; }

Then I simply added a ~\Views\Shared\EditorTemplates\ReadOnly.ascx file to my project with this in it:

然后我简单地在其中添加了一个〜\ Views \ Shared \ EditorTemplates \ ReadOnly.ascx文件到我的项目中:

<%= Model %>

A really simple way to add custom templates, you could include formatting or whatever.

添加自定义模板的一种非常简单的方法,您可以包括格式或其他。

#3


2  

<%@ Control Language="C#" Inherits="ViewUserControl<Product>" %>

<%: Html.LabelFor(x => x.ProductID) %>
<%: Html.TextBoxFor(x => x.ProductID, new { @readonly = true }) %>

<%: Html.LabelFor(x => x.Name) %>
<%: Html.TextBoxFor(x => x.Name) %>