asp.net-mvc-3 – 如何从@ Html.EditForModel()中排除一个字段,
发布时间:2020-12-16 03:41:16 所属栏目:asp.Net 来源:网络整理
导读:我正在阅读ASP.NET MVC及其所有有趣的用途,我刚刚发现了大约 DataTemplates. 我急于测试这个东西,我将一个更简单的模型转换为使用@ Html.DisplayForModel()和@ Html.EditForModel(),它就像一个幸运的魅力,它是:) 我立即发现的一件事是,我无法轻松定义一个字
我正在阅读ASP.NET MVC及其所有有趣的用途,我刚刚发现了大约
DataTemplates.
我急于测试这个东西,我将一个更简单的模型转换为使用@ Html.DisplayForModel()和@ Html.EditForModel(),它就像一个幸运的魅力,它是:) 我立即发现的一件事是,我无法轻松定义一个字段来显示在显示视图上,但根本不存在用于编辑…… 解决方法
您可以使用IMetadataAware接口创建属性,该属性将在元数据中设置ShowForEdit和ShowForDislay:
[AttributeUsage(AttributeTargets.Property,AllowMultiple = false,Inherited = true)] public class TemplatesVisibilityAttribute : Attribute,IMetadataAware { public bool ShowForDisplay { get; set; } public bool ShowForEdit { get; set; } public TemplatesVisibilityAttribut() { this.ShowForDisplay = true; this.ShowForEdit = true; } public void OnMetadataCreated(ModelMetadata metadata) { if (metadata == null) { throw new ArgumentNullException("metadata"); } metadata.ShowForDisplay = this.ShowForDisplay; metadata.ShowForEdit = this.ShowForEdit; } } 然后您可以将它附加到您的财产,如下所示: public class TemplateViewModel { [TemplatesVisibility(ShowForEdit = false)] public string ShowForDisplayProperty { get; set; } public string ShowAlwaysProperty { get; set; } } 这就是你所需要的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 缺少Visual Studio 2012 MVC本地数据库添加
- asp.net mvc 下载文件并不打开下载页面
- ASP.NET MVC 3应用程序中每个浏览器选项卡/窗口的新会话
- asp.net-mvc-3 – 使用AutoMapper携带元数据查看模型的技术
- asp.net-mvc – MVC 3 Ajax.ActionLink不能正常工作
- asp.net-mvc – 设置焦点在文本框 – MVC3
- asp.net – 没有Eval的GridGroupHeaderItem.AggregatesValu
- ASP.Net添加控件“无法获取[控件]的内部内容,因为内容不是文
- 将我的思维模式从ASP.NET迁移到ASP.NET MVC(2)时需要了解哪
- asp.net – 你如何确定哪个验证器失败?
推荐文章
站长推荐
- Asp.net MVC scheduler的实现方法详解
- asp.net-mvc – ASP MVC单选按钮验证
- 如何使用ASP.NET MVC引用CSS样式?
- asp.net – 为什么我收到此错误:System.Securit
- asp.net – IE10中的LinkBut??tons不执行回发
- asp.net – 有没有办法使VS2010发布向导在发布网
- asp.net – 从域到www.domain的全局301重定向
- ASP.NET OutputCache varyByParam在Web.config中
- asp.net-mvc – MVC5基于声明的身份验证 – 适用
- ASP.NET Web API项目自定义接口路由和HTTP请求方
热点阅读