@ Html.Editor在使用反射属性时显示奇怪的结果

时间:2022-09-04 08:22:08

I'm using a generic Razor view to allow any entity framework object to be edited. Here's a cut down version of it:

我正在使用通用Razor视图来允许编辑任何实体框架对象。这是它的简化版本:

@model Object
@using (Html.BeginForm())
{
        @foreach (var property in Model.VisibleProperties())
        {
            @Html.Label(property.Name.ToSeparatedWords())
            @Html.Editor(property.Name, new { @class = "input-xlarge" })
        }
}

And the VisibleProperties() function goes like this:

VisibleProperties()函数如下所示:

public static PropertyInfo[] VisibleProperties(this Object model)
        {
            return model.GetType().GetProperties().Where(info => 
                (info.PropertyType.IsPrimitive || info.PropertyType.Name == "String") &&
                info.Name != model.IdentifierPropertyName()).OrderedByDisplayAttr().ToArray();
        }

(I'm reusing code from https://github.com/erichexter/twitter.bootstrap.mvc/)

(我正在重用https://github.com/erichexter/twitter.bootstrap.mvc/中的代码)

One of my sample controllers goes as follows:

我的一个示例控制器如下:

  public ActionResult Edit(int id = 0)
        {
            TaskTemplate tasktemplate = db.TaskTemplates.Single(t => t.TaskTemplateID == id);
            return View(tasktemplate);
        }

Now the problem: It all works fine except for where there's an ID property that relates to a 'parent' table, such as UserID. For these fields, the output of the @Html.Editor is simply: FalseFalseFalseTrueFalse.

现在的问题是:一切正常,除了有一个与'父'表相关的ID属性,比如UserID。对于这些字段,@ Html.Editor的输出只是:FalseFalseFalseTrueFalse。

The True seems to correspond to the user in question - in this case the 4th user in the database.

True似乎对应于有问题的用户 - 在这种情况下是数据库中的第4个用户。

Why is it not ouputting a nice textbox with the number 4 (or whatever the UserID) is in it?

为什么它没有输出一个带有数字4(或者用户ID)的文本框呢?

I hope I've explained this clearly.

我希望我已经清楚地解释了这一点。

1 个解决方案

#1


1  

The reason for that is because editor/display templates are not recursing into complex child objects. If you want this to happen you could write a custom editor template for the object type (~/Views/Shared/Object.cshtml) as illustrated by Brad Wilson in this blog post (more specifically the Shallow Dive vs. Deep Dive section towards the end).

原因是编辑器/显示模板没有递归到复杂的子对象中。如果你想要这样做,你可以为对象类型(〜/ Views / Shared / Object.cshtml)编写一个自定义编辑器模板,如Brad Wilson在这篇博客文章中所示(更具体地说是浅水潜水与深潜潜水部分)结束)。

So:

所以:

<table cellpadding="0" cellspacing="0" border="0">
@foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) 
{
    if (prop.HideSurroundingHtml) 
    {
        @Html.Editor(prop.PropertyName)
    }
    else 
    {
        <tr>
            <td>
                <div class="editor-label" style="text-align: right;">
                    @(prop.IsRequired ? "*" : "")
                    @Html.Label(prop.PropertyName)
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @Html.Editor(prop.PropertyName)
                    @Html.ValidationMessage(prop.PropertyName, "*")
                </div>
            </td>
        </tr>
    }
}
</table>

#1


1  

The reason for that is because editor/display templates are not recursing into complex child objects. If you want this to happen you could write a custom editor template for the object type (~/Views/Shared/Object.cshtml) as illustrated by Brad Wilson in this blog post (more specifically the Shallow Dive vs. Deep Dive section towards the end).

原因是编辑器/显示模板没有递归到复杂的子对象中。如果你想要这样做,你可以为对象类型(〜/ Views / Shared / Object.cshtml)编写一个自定义编辑器模板,如Brad Wilson在这篇博客文章中所示(更具体地说是浅水潜水与深潜潜水部分)结束)。

So:

所以:

<table cellpadding="0" cellspacing="0" border="0">
@foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) 
{
    if (prop.HideSurroundingHtml) 
    {
        @Html.Editor(prop.PropertyName)
    }
    else 
    {
        <tr>
            <td>
                <div class="editor-label" style="text-align: right;">
                    @(prop.IsRequired ? "*" : "")
                    @Html.Label(prop.PropertyName)
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @Html.Editor(prop.PropertyName)
                    @Html.ValidationMessage(prop.PropertyName, "*")
                </div>
            </td>
        </tr>
    }
}
</table>