2011-11-18 29 views
27

我在我的項目中有一個枚舉,並且爲此枚舉創建了一個自定義編輯器模板。所以,現在我有一個屬性與這種枚舉類型的任何模型,將呈現一個下拉。如何在我的MVC3自定義編輯器模板中獲取屬性名稱

這很好,但我想用屬性的名稱命名我的下拉列表中的select元素。這是我的編輯器模板的Razor代碼。

@model ItemEnumerations.ItemType 

    <select id="PropertyNameHere" name="PropertyNameHere"> 
    @foreach (ItemEnumerations.ItemType in Enum.GetValues(typeof(ItemEnumerations.ItemType))) { 
     <option value="@value" @(Model == @value ? "selected=\"selected\"" : "")>@value.ToString()</option>   
    } 
    </select> 

所以,在這裏我有「PropertyNameHere」爲選擇元素id和name屬性,我想有我的模型的屬性的名稱。這裏有一個例子:

我的模型:

public class MyModel{ 
     public int ItemID {get;set;} 
     public string ItemName {get;set;} 
     public ItemEnumerations.ItemType MyItemType {get;set;} 
    } 

我查看代碼:

@model MyModel 

@Html.LabelFor(m => model.ItemID) 
@Html.DisplayForm(m => model.ItemID) 

@Html.LabelFor(m => model.ItemName) 
@Html.EditorFor(m => model.ItemName) 

@Html.LabelFor(m => model.MyItemType) 
@Html.EditorFor(m => model.MyItemType) 

我想我的選擇元素有 'MyItemType' 的名稱和ID。

+0

在編輯器模板獲得屬性的名稱也許更好的解決辦法是延長DropDownFor方法來處理我的枚舉? –

+0

試試這個...http://blog.nathan-taylor.net/2012/10/aspnet-mvc-get-field-name-and-id-for.html –

回答

51

我發現了一本書,我這裏有答案。事實上,它讓我感覺很親密,但是我可以根據我發現的情況來搜索其他人。

這是我需要添加到我的編輯模板。

@{var fieldName = ViewData.TemplateInfo.HtmlFieldPrefix;} 
<select id="@fieldName" name="@fieldName"> 
+2

tks傑夫,在這之後,我發現了方法GetFullHtmlFieldName()... ViewData.TemplateInfo.GetFullHtmlFieldName(「field」) – Gadonski

+3

如果您使用編輯器的默認模板,您也可以通過'ViewData.ModelMetadata .PropertyName'。但是,使用您提出的解決方案使用默認模板或指定模板名稱(即'@ Html.EditorFor(m => m.ItemName,「templatename」)')。 –

-1

您可以創建一個幫手來使用你的觀點,這說明here

你可以使用這段代碼來獲取名稱裏面。

public static class GenericHelper<T> 
{ 
    public static String GetPropertyName<TValue>(Expression<Func<T, TValue>> propertyId) 
    { 
     var operant = (MemberExpression)((UnaryExpression)propertyId.Body).Operand; 
     return operant.Member.Name; 
    } 
} 

我認爲缺省的助手做同樣的,如HiddenFor,因爲他們也使用Expression<Func<TModel, TProperty>>

+0

我希望通過一些元數據獲取屬性名稱。我想盡可能通用。 –

+0

使用Expression <>確實保持它的通用性? 在您的視圖中,您將使用@Http.YourHelper(m => model.ItemID),其中您的助手從代碼片段 –

+0

中返回GetPropertyName,請參閱我對@Todd的評論,此處也適用。 – chiccodoro

4

如何下面的模板:

@model ItemEnumerations.ItemType 
@{ 
    var values = 
     from value in Enum.GetValues(typeof(ItemType)).Cast<ItemType>() 
     select new { ID = (int)value, Name = value.ToString() }; 
    var list = new SelectList(values , "ID", "Name", (int)Model); 
} 
@Html.DropDownList("", list) 

這樣,您就不需要手動渲染<select><option>標籤和你重用現有DropDownList幫手。

+0

這與我的代碼一起工作(請參閱我對此問題的回答),但我放棄瞭解決方案。我喜歡使用現有的幫手。再次感謝Darin。 –

19

爲了將來的參考(老問題),我發現這個:System.Web.Mvc.Html.NameExtensions。

使用這些,你可以這樣做

<input type=text" name="@Html.NameFor(m => m.MyProperty)"> 

,你會得到

<input type=text" name="MyProperty"> 

有在此擴展類其他幾個相關的幫手。不過,這種方法不僅僅是獲取屬性名稱。例如,你可以使用m.MyProperty.MySubProperty,你會得到一個有效的HTML名稱來發布。

+1

很高興知道。儘管如此,它仍然沒有回答這個問題,因爲您仍然需要知道名稱MyProperty才能使用它。但是如果你調用'EditorFor(m => m.MyProperty)'並且在Editor模板中你想知道它被調用的名字是'MyProperty',這沒有幫助。 – chiccodoro

+0

這解決了我獲取嵌套屬性名稱的問題。 – jahu

2

您可以通過

@{ 
    string name = ViewData.ModelMetadata.PropertyName; 
} 
相關問題