English 中文(简体)
在伙伴关系中使用元数据。 NET MVC
原标题:Using the Description Metadata Attribute in ASP.NET MVC

我正在研究一个VB。 NET项目正在使用ASP。 NET MVC 2. 结 论 我利用我模型中元数据添加验证和其他属性的能力。

例如,我增加了以下特性:<DisplayName(“未来名称”)>对我模型中的财产作了说明,并且正在使用将这些特性加起来。 Html.LabelFor ( extension methods.

我也添加了<代码><Description (" this is a presentation of the field >>属该模型中的各种特性,并希望以类似方式说明这些特性。

我的问题是——是否存在一种延伸方法,对我来说是这样吗?

如果有的话,有人会引导我正确撰写我自己的文件? I ve以现有方法的签署方法为依据......

<ExtensionAttribute()> _
Public Function HintFor(Of TModel, TProperty) _
                       (ByVal htmlHelper As HtmlHelper(Of TModel), _
                        ByVal expression As Expression(Of Func(Of TModel, TProperty))) As MvcHtmlString

End Function

但我必须承认这部分内容远远超出了我的Lambda /LINQ /? 现阶段的知识!

预告......

最佳回答

如果你重新使用MVC 3,你可以使用<代码>DisplayAttribute,并且仅使用描述参数。

Public Class User
    <Display(Name = "User name", Description = "This is a description")> _
    Public Property Name As String
End Class


<System.Runtime.CompilerServices.Extension> _
Public Shared Function HintFor(Of TModel, TValue)(html As HtmlHelper(Of TModel), expression As Expression(Of Func(Of TModel, TValue))) As IHtmlString
    Dim attribute = ModelMetadata.FromLambdaExpression(Of TModel, TValue)(expression, html.ViewData)

    Return MvcHtmlString.Create(attribute.Description)
End Function

Mvc2

我只是经过了一个奇怪的考验,看看看这一工程是否可行。 (我不经常使用VB并使用在线转换器) 没有任何错误或任何错误,但会产生预期的结果。

<System.Runtime.CompilerServices.Extension()> _
Public Shared Function HintFor(Of TModel, TValue)(html As HtmlHelper(Of TModel),     expression As Expression(Of Func(Of TModel, TValue))) As IHtmlString

    Dim ex As MemberExpression = DirectCast(expression.Body, MemberExpression)
    For Each attribute As Attribute In ex.Expression.Type.GetProperty(ex.Member.Name).GetCustomAttributes(True)
        If GetType(System.ComponentModel.DescriptionAttribute) = attribute.[GetType]() Then
            Return MvcHtmlString.Create(DirectCast(attribute, System.ComponentModel.DescriptionAttribute).Description)
        End If
    Next

    Dim x = ModelMetadata.FromLambdaExpression(Of TModel, TValue)(expression, html.ViewData)
    Return MvcHtmlString.Create(x.Description)
End Function

Additional

我确实不敢肯定,为什么在你能够这样做的时候,我就这样做了。 (尽管我称“MVC 2”可能无法与数据发布妥善合作)

<System.Runtime.CompilerServices.Extension()> _
Public Shared Function HintFor(Of TModel, TValue)(html As HtmlHelper(Of TModel),     expression As Expression(Of Func(Of TModel, TValue))) As IHtmlString
    Dim x = ModelMetadata.FromLambdaExpression(Of TModel, TValue)(expression, html.ViewData)
    Return MvcHtmlString.Create(x.Description)
End Function
问题回答

此处为C#的更完整的版本I,也支持htmlAttribut:

public static MvcHtmlString DescriptionFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
    var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    var description = metadata.Description;

    RouteValueDictionary anonymousObjectToHtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

    TagBuilder tagBuilder = new TagBuilder("span");
    tagBuilder.MergeAttributes<string, object>(anonymousObjectToHtmlAttributes);
    tagBuilder.SetInnerText(description);

    return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
}

与此类似:

[Display(Name = "Style", Description = "Some description of this field")]
public float Style { get; set; }

@Html.DescriptionFor(model => model.Style, new { @class = "help-inline" })




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签