Nested editor name to display / override name

Overriding ViewData.TemplateInfo.HtmlFieldPrefixwith an empty string is unacceptable , is there a way so that the prefix does not penetrate the nested set of strongly typed calls for the editor or DisplayFor?

Here is the ugly markup / code I'm still working with:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Web.Mvc.SelectList>" %>
<% ViewData.TemplateInfo.HtmlFieldPrefix = ""; %>
<%=Html.DropDownList("sort", Model)%>

I tried * For overload, which allows you to specify htmlFieldName, but this only changes the immediate level. If I have a prefix at the point of this call, I just change what is added to the prefix.

I could write the markup of the template manually, but to do this for the object SelectListit seems that I will just finish copying through the MVC source with one setting, since it is connected with the data binding logic of the object.

+5
source share
2 answers

If you have a property in your view model, and not just a drop-down list directly on the model, you can put the DataAnnontation attribute on it.

public class MyModel
{
   [Display(Name="Your Favorite Choices")]
   public string[] Choices {get; set;}
}

then in your code

<%= Html.LableFor(m => m.Choices) %><br />
<%=Html.DropDownList("sort", Model.Choices)%>

Must use this name.

[Hope I got it right from memory. :)]

Here's the MSDN link: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayattribute.displayattribute.aspx

0
source

I'm not sure I understand your question, but if you present the "child" views as partial, not EditorFor, then the fields will not be prefixes.

0
source

All Articles