遍历模型属性在反思,然后使用HTML辅助显示。如何获得具体的财产回来? [英] Loop through Model properties in reflection, then use Html helpers to display. How to get concrete property back?

查看:161
本文介绍了遍历模型属性在反思,然后使用HTML辅助显示。如何获得具体的财产回来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有我们试图通过模型的属性使用反射来循环的MVC4 ASP.Net网站,并显示名称/价值/使用HTML辅助等信息。

We have an MVC4 ASP.Net site that we are trying to use reflection to loop through properties of a model, and display the names/values/and other information using Html helpers.

我们有一个自定义的HTML帮助我们在争论通过从下面的方法。

We have a custom Html Helper that we are passing in arguments from the method below.

@foreach (PropertyInfo prop in Model.GetType().GetProperties())
{
    <div class="form-group">
        Html.LabelFor( ?? Any ideas ?? )
        <div class="col-sm-9">
            @SuperEditorFor.ReflectiveEditorFor(prop, Model)
            @Html.ValidationMessageFor(model => model.GetType().GetProperty(prop.Name))
        </div>
    </div>
}

我们试图把在财产(引号),如 ValidationMessageFor 但我们怀疑,它所需要的实际具体属性,而不是反射PropertyInfo对象。

We have tried putting in the "property" (quote) as in the ValidationMessageFor but as we suspected, it wants the actual concrete property, not the reflection propertyInfo object.

有谁知道这是否可能?有没有人尝试过​​这样做吗?

Does anyone know if this is possible? Has anyone tried to do this before?

推荐答案

您不必使用通用的实现为( EditorFor DisplayFor ...),只需使用非一般的人,编辑显示。 ..

You don't need to use generic implementations for that (EditorFor, DisplayFor...), just use the non generic ones, Editor, Display...

这会工作得很好,你会得到验证,自动绑定和其他事物一样,整个九码...

This will work just fine, you will get validation, automatic bindings and everything else, the whole nine yards...

@foreach (PropertyInfo prop in Model.GetType().GetProperties())
{
    <div class="form-group">
        @Html.Label(prop.Name)
        <div class="col-sm-9">
            @Html.Editor(prop.Name)
            @Html.ValidationMessage(prop.Name)
        </div>
    </div>
}

如果你想实现的通用实验这里是关于如何做一个伟大的博客帖子

If you want to experiment with generic implementations here is a great blog post on how to do that

<一个href=\"http://www.joels$c$c.com/use-mvc-templates-with-dynamic-generated-types-with-custom-htmlhelper-extensions/\" rel=\"nofollow\">http://www.joels$c$c.com/use-mvc-templates-with-dynamic-generated-types-with-custom-htmlhelper-extensions/

这篇关于遍历模型属性在反思,然后使用HTML辅助显示。如何获得具体的财产回来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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