我如何获得在EditorTemplate完全合格成员的名字吗? [英] How do I get the the fully qualified member name in an EditorTemplate?

查看:178
本文介绍了我如何获得在EditorTemplate完全合格成员的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC 4个网站,我路过一个嵌套的属性设置为一个EditorTemplate并使用 ViewData.ModelMetadata.PropertyName 然而,这得建立字段名子属性的属性名称,而不是财产的全部点符号的名字我编辑。

I have an ASP.NET MVC 4 site and I am passing a nested property to an EditorTemplate and building the field name using ViewData.ModelMetadata.PropertyName however, this gets the property name of the child property, not the full dot-notation name of the property I'm editing.

一个例子可以说明最好的:

An example will illustrate best:

由于以下类型的

public class EditEntityViewModel
{
     // elided
     public Entity Entity { get; set}
}


public class Entity 
{
    // elided
    public string ImageUrl { get; set; }
}

我的编辑和创建的看法如下:

My Edit and Create views are as follows:

@model EditEntityViewModel

<div>
    @Html.EditorFor(model => model.Entity.ImageUrl , "_ImageUploader")
</div>

该_ImageUploader.cshtml编辑模板如下:

The _ImageUploader.cshtml Editor Template is as follows

@model System.String

<div class="uploader">
    @Html.LabelFor(model => model)
    @Html.HiddenFor(model => model)
    <div data-rel="@(ViewData.ModelMetadata.PropertyName)">   
        <input type="file" name="@(ViewData.ModelMetadata.PropertyName)Upload" />
    </div>
</div>

渲染HTML看起来像这样

The rendered HTML looks like this

<div>
   <div class="uploader">
        <label for="Entity_ImageUrl">Image</label>
        <input id="Entity_ImageUrl" name="Entity.ImageUrl" type="hidden"/>
        <div data-rel="ImageUrl"> <!-- Problem -->
             <input type="file" name="ImageUrlUpload" /><!-- Problem -->
        </div>
    </div>
</div>

这是5号线与6,在那里我已经叫 ViewData.ModelMetadata.PropertyName ,我只是让孩子属性名称的通知的ImageUrl,但我想Entity.ImageUrl。如何获得获得这个全名?

Notice on line 5 and 6, where I've called ViewData.ModelMetadata.PropertyName, I'm only getting the child property name "ImageUrl" but I want "Entity.ImageUrl". How do get get this fully qualified name?

推荐答案

使用 @ Html.NameFor(型号=&GT;模型) / @ Html.NameForModel()而不是 ViewData.ModelMetadata.PropertyName ,这种方法应该给你想要的名称。它 Mvc3Futures 的包的一部分,并加入到MVC4

Use @Html.NameFor(model => model) / @Html.NameForModel() instead of ViewData.ModelMetadata.PropertyName, this method should give you desired name. It's part of Mvc3Futures package and was added to MVC4.

这篇关于我如何获得在EditorTemplate完全合格成员的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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