ReadOnly(true) 是否可以与 Html.EditorForModel 一起使用?
考虑以下设置:
模型:
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"
});
}
}
结果是这样的:
我原以为 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过向“ReadOnly”类的属性添加 UIHintAttribute 解决了这个问题。
然后我只是将一个 ~\Views\Shared\EditorTemplates\ReadOnly.ascx 文件添加到我的项目中,其中包含以下内容:
添加自定义模板的非常简单的方法,您可以包含格式或其他内容。
I solved this problem by adding a UIHintAttribute to the property on my class of "ReadOnly".
Then I simply added a ~\Views\Shared\EditorTemplates\ReadOnly.ascx file to my project with this in it:
A really simple way to add custom templates, you could include formatting or whatever.
ReadOnly
和Required
属性将由元数据提供程序使用,但不会被使用。如果您想使用EditorForModel
删除输入,则需要自定义模板或[ScaffoldColumn(false)]
。对于自定义模板
~/Views/Home/EditorTemplates/Product.ascx
:另请注意,默认模型绑定器不会将值复制到具有
[ReadOnly(false)]
的属性中代码>.此属性不会影响默认模板呈现的 UI。The
ReadOnly
andRequired
attributes will be consumed by the metadata provider but won't be used. If you want to get rid of the input withEditorForModel
you'll need a custom template, or[ScaffoldColumn(false)]
.For custom template
~/Views/Home/EditorTemplates/Product.ascx
: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.