MVC3 - 在 html.helper 中获取数据属性
我正在尝试创建一个自定义属性来控制自定义 HTML 帮助器对象中的格式。我的自定义选择器类的源代码是(代码来自 http://forums. asp.net/t/1649193.aspx/1/10)。
public static MvcHtmlString DdUovFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<string, object> htmlAttributes)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var m = expression.Body.GetType();
IDictionary<string, object> validationAttributes = htmlHelper
.GetUnobtrusiveValidationAttributes(ExpressionHelper.GetExpressionText(expression), metadata);
if (htmlAttributes == null)
htmlAttributes = validationAttributes;
else
htmlAttributes = htmlAttributes.Concat(validationAttributes).ToDictionary(k => k.Key, v => v.Value);
return SelectExtensions.DropDownListFor(htmlHelper, expression,
selectList, optionLabel, htmlAttributes);
}
我的自定义属性是
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property |
AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class LookUpAttribute : Attribute
{
public LookUpAttribute(Type providerName)
{
this.providerName = providerName;
}
protected ILookup providerName;
}
我的问题是我无法弄清楚如何在 HTML 帮助程序方法中检索我的自定义属性。
I'm trying to create a custom attribute to control formatting in a custom HTML helper object. The source code for my custom selector class is (Code is from http://forums.asp.net/t/1649193.aspx/1/10).
public static MvcHtmlString DdUovFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<string, object> htmlAttributes)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var m = expression.Body.GetType();
IDictionary<string, object> validationAttributes = htmlHelper
.GetUnobtrusiveValidationAttributes(ExpressionHelper.GetExpressionText(expression), metadata);
if (htmlAttributes == null)
htmlAttributes = validationAttributes;
else
htmlAttributes = htmlAttributes.Concat(validationAttributes).ToDictionary(k => k.Key, v => v.Value);
return SelectExtensions.DropDownListFor(htmlHelper, expression,
selectList, optionLabel, htmlAttributes);
}
My custom attribute is
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property |
AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class LookUpAttribute : Attribute
{
public LookUpAttribute(Type providerName)
{
this.providerName = providerName;
}
protected ILookup providerName;
}
My problem is that I cannot figure out how to retrieve my custom attribute in in the HTML helper method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现此目的的最佳方法是让您的属性实现
IMetadataAware
。在OnMetadataCreated
方法的实现中,您应该将值添加到metadata.AdditionalValues
字典中。然后,您可以在助手中或您有权访问 ModelMetadata 的其他任何地方检索该值。请注意,此解决方案是首选,因为它实际上不会对有关元数据由属性驱动这一事实的任何信息进行编码。
The best way to achieve this is for your attribute to implement
IMetadataAware
. In the implementation of theOnMetadataCreated
method you should add values to themetadata.AdditionalValues
dictionary. You can then retrieve the value in your helper or wherever else you have access to ModelMetadata.Note that this solution is prefered because it does not actually encode any information about the fact that your metadata is driven by attributes.