更改 MVC DisplayFor 以不显示 Id 字段
我尝试使用 MVC 3 中的模板功能来更轻松地为一系列对象创建查看/编辑屏幕。
我首先简单地使用 Html.DisplayForModel() - 现在我想自定义模型中的不同字段,以便相同类型的字段始终以相同的方式显示。无论如何,这就是想法 - 我仍在尝试弄清楚模板到底有多有用。
现在,如果我只使用 Html.DisplayForModel,它只会执行一个带有字段名称的 div 和另一个带有值的 div。例如,Name 属性将呈现为
<div class="display-label">Name</div>
<div class="display-field">Bob</div>
Ok,我可以使用它 - 但例如 Id 字段也将以这种方式显示。因此,我尝试根据 这个博客。问题是,如果我这样做(我将 Id 字段更改为隐藏输入),它仍然会将标签 div 放在那里。
是否可以控制每个属性的标签和字段 div?或者我是否必须执行整个模板(而不仅仅是自定义字段)?
I'm trying to use the templating functionality in MVC 3 to make it easier to create view/edit screens for a range of objects.
I've started by simply using Html.DisplayForModel() - I would now like to customize the different fields in my Model so that the same type of field would always be displayed in the same way. That's the idea anyways - I'm still trying to figure out how useful templating really is.
Now if I just use Html.DisplayForModel it just does a div with the name of the field and another div with the value. So for example a Name property will get rendered as
<div class="display-label">Name</div>
<div class="display-field">Bob</div>
Ok, I can work with that - but for example an Id field will also be displayed that way. So I tried to override how the Id field will be displayed according to this blog. Problem is, if I do that (I changed the Id field to be a hidden input) it still puts the label div in there.
Is it possible to control both the label and field divs for each property? Or do I have to do the entire template (instead of just customizing the field)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 Id 属性上设置 ScaffoldColumn 属性。请在此处查看更多详细信息。
You can set ScaffoldColumn attribute on Id property. See more details here.