Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the property name in my MVC3 custom Editor Template

I have an enumeration in my project and I've created a custom editor template for this enumeration. So, now any model I have with a property with a type of this enumeration, will render a drop down.

This works great, but I would like to name my select element of my dropdown with the name of the property. Here is my Razor code for my editor template.

    @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>

So, where I have 'PropertyNameHere' for the select elements id and name attributes, I would like to have the name of the property of my model. Here is an example:

My Model:

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

My View Code:

@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 )

I would like my select element to have a name and id of 'MyItemType'.

like image 469
Jeff Reddy Avatar asked Nov 18 '11 16:11

Jeff Reddy


4 Answers

I found the answer in a book I have here. Actually, it got me close, but I then could google the rest based on what I found.

Here is what I needed to add to my editor template.

@{var fieldName = ViewData.TemplateInfo.HtmlFieldPrefix;}
<select id="@fieldName" name="@fieldName"> 
like image 191
Jeff Reddy Avatar answered Nov 15 '22 13:11

Jeff Reddy


For future reference (old question), I found this: System.Web.Mvc.Html.NameExtensions.

Using those, you can do something like

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

And you'll get

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

There are several other related helpers in this extension class. This method does more than just get the property name, though. For example, you could use m.MyProperty.MySubProperty and you'd get a valid HTML name for posting.

like image 32
Todd Avatar answered Nov 15 '22 15:11

Todd


How about the following template:

@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)

This way you don't need to manually render <select> and <option> tags and you are reusing the existing DropDownList helper.

like image 4
Darin Dimitrov Avatar answered Nov 15 '22 13:11

Darin Dimitrov


You can get the property name in the editor template via

@{
   string name = ViewData.ModelMetadata.PropertyName;
}
like image 2
MikeT Avatar answered Nov 15 '22 13:11

MikeT