ASP.Net MVC(2) - 获取ModelMetadata从View没有自定义模板 [英] ASP.Net MVC (2) - Getting ModelMetadata from a View without a custom template

查看:78
本文介绍了ASP.Net MVC(2) - 获取ModelMetadata从View没有自定义模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何(如果可能)从视图获取到ModelMetadata而不为给定模型的属性自定义模板?

How can I (if possible) get to the ModelMetadata from a view without creating a custom template for the given Model's property?

传统上,我刚刚作出了一个新的模板,并改变使用ViewData.ModelMetadata.IsRequired(例如)的模板。不过,我正在寻找如何在基础视图指定的编辑器模板中访问这些信息,而不是

Traditionally, I have just made a new template and altered the template using the ViewData.ModelMetadata.IsRequired (for example). However, I'm looking for how to access this information in the base view and not within a specific editor template.

在为什么我不想去编辑模板路线方面 - 我有一个表格,我需要在输入要素属性方面更好地控制标记。 EditorTemplates体面的工作,但有一个专门的模板,并确保更多的ViewData被设置为满足属性值在我看来是不干净。

In terms of why I don't want to go the editor template route - I have a form where I need better control to the markup in terms of attributes on the input elements. EditorTemplates work decent but having a specialized template and ensuring additional ViewData are set to fulfill the attribute values isn't clean in my opinion.

推荐答案

这应该给你访问:

<%
    var metadata = ViewData.ModelMetadata;
%>

您可以签如何使用<一个href=\"http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html\"相对=nofollow>默认模板实现。它可能给你一些额外的想法。

You may checkout how the default templates are implemented. It might give you some additional ideas.

这篇关于ASP.Net MVC(2) - 获取ModelMetadata从View没有自定义模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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