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

前端 未结 6 985
粉色の甜心
粉色の甜心 2021-02-01 02:41

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, wi

相关标签:
6条回答
  • 2021-02-01 02:54

    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.

    0 讨论(0)
  • 2021-02-01 03:00

    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"> 
    
    0 讨论(0)
  • 2021-02-01 03:03

    You can get the property name in the editor template via

    @{
       string name = ViewData.ModelMetadata.PropertyName;
    }
    
    0 讨论(0)
  • 2021-02-01 03:11

    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.

    0 讨论(0)
  • 2021-02-01 03:16

    You can create a helper to use that inside your view, which is explained here

    And you can use this piece of code to get the name.

     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;
         }
    }
    

    I assume the default helpers do the same, such as HiddenFor, as they also use Expression<Func<TModel, TProperty>>

    0 讨论(0)
  • 2021-02-01 03:19

    As mentionned in the accepted answer, ViewData.TemplateInfo.HtmlFieldPrefix gives you the property name inside an EditorTemplate.

    I think it is worth saying that you can use an empty string inside the HtmlHelper functions if your goal is to generate an input/select. It will use the model's property name.

    @Html.TextBox("", theValue, new { @class = "form-control" })
    

    or

    @Html.DropDownList("", new List<SelectListItem>(), theValue, new { @class = "form-control" })
    
    0 讨论(0)
提交回复
热议问题