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

发布于 2024-09-14 11:57:34 字数 1203 浏览 2 评论 0原文

考虑以下设置:

模型:

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

    public string Name
    {
        get;
        set;
    }
}

视图:

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

控制器:

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

结果是这样的: alt text

我原以为 ProductID 属性无法通过 ReadOnly(true) 属性。支持吗?如果没有,有什么方法可以提示 ASP.NET MVC 我的模型的某些属性是只读的吗?我不想通过 [ScaffoldColumn(false)] 隐藏 ProductID

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:
alt text

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你的占有欲 2024-09-21 11:57:34

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

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

然后我只是将一个 ~\Views\Shared\EditorTemplates\ReadOnly.ascx 文件添加到我的项目中,其中包含以下内容:

<%= Model %>

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

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

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

<%= Model %>

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

行至春深 2024-09-21 11:57:34

ReadOnlyRequired 属性将由元数据提供程序使用,但不会被使用。如果您想使用 EditorForModel 删除输入,则需要自定义模板或 [ScaffoldColumn(false)]

对于自定义模板 ~/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) %>

另请注意,默认模型绑定器不会将值复制到具有 [ReadOnly(false)] 的属性中代码>.此属性不会影响默认模板呈现的 UI。

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

For custom template ~/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.

妄断弥空 2024-09-21 11:57:34
<%@ 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) %>
<%@ 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) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文