为什么会出现[显示(AutoGenerateField = FALSE)]和[HiddenInput(DisplayValue = FALSE)] [英] Why is there a [Display(AutoGenerateField=false)] and a [HiddenInput(DisplayValue = false)]?

查看:734
本文介绍了为什么会出现[显示(AutoGenerateField = FALSE)]和[HiddenInput(DisplayValue = FALSE)]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题有点说这一切。

有似乎是一个标记字段作为带有属性请不要在UI上显示两种方式。一位住在DataAnnotations命名空间,这是我觉得它属于和其他生活在具体的MVC-命名空间System.Web.Mvc,我认为这是错误的地方。

There appears to be two ways to mark a field as "Please don't show on the UI" with attributes. One lives in the DataAnnotations namespace, which is where I think it belongs, and the other lives in the MVC-specific namespace System.Web.Mvc, which I think is the wrong place.

这意味着,对于一个MVC应用程序,我有我的脏域类与MVC的命名空间,而不是用更通用的ComponentModel.DataAnnotations。

It means that for an MVC app, I have to dirty my domain class with the MVC namespace, rather than use the "more generic" ComponentModel.DataAnnotations.

有什么办法有显示()属性的MVC框架留意了?

Is there any way to have the MVC framework take notice of the Display() attribute?

推荐答案

虽然你真的应该注意保罗的答案重新:使用的ViewModels,您的问题的常见解决方案是与生活在该UIHint属性来装饰你的财产System.ComponentModel.DataAnnotations命名空间。

Although you should really take note of Paul's answer re: using ViewModels, a common solution to your problem is to decorate your property with the UIHint attribute which lives in the System.ComponentModel.DataAnnotations namespace.

例如

[UIHint("Hidden")]

然后在共享添加一个小视图模板\\ EditorTemplates \\ Hidden.cshtml像

Then add a small view template in Shared\EditorTemplates\Hidden.cshtml like

@model object
@Html.HiddenFor( x => x)

对于模板的名称UIHint给出的字符串匹配

Where the name of the template matches the string given in UIHint

这篇关于为什么会出现[显示(AutoGenerateField = FALSE)]和[HiddenInput(DisplayValue = FALSE)]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆