Asp.net mvc 2 templates without prefix

Given the following view model:

class DetailsViewModel
{
   public HeaderViewModel Header {get;set;}
   public FooterViewModel Footer {get;set;}
}

      

I am using editor template for header view model:

<%: Html.EditorFor(x => x.Header) %>

      

Editor Template (EditorTemplates / HeaderViewModel.ascx)

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<HeaderViewModel>" %>

<% ViewData.TemplateInfo.HtmlFieldPrefix = ""; %>

<%: Html.EditorFor(x => x.Search) %>

      

Result:

<input type="text" value="" name="Search" id="Search" />

      

If I remove the line

<% ViewData.TemplateInfo.HtmlFieldPrefix = ""; %>

      

result:

<input type="text" value="" name="Header.Search" id="Header_Search" />

      

Is there another way to achieve the same - render the control names without a prefix?

I was thinking about the helper:

public static MvcHtmlString EditorWithoutPrefix<TModel, TValue>(
  this HtmlHelper<TModel> html, TValue value)
{
  var htmlHelper =... // create new HtmlHelper<TValue> and set it model to be 'value' argument

  return htmlHelper.EditorForModel();
}

      

and use it:

<%: Html.EditorWithoutPrefix(Model.Header) %>

      

but it throws exceptions.

Or maybe you know another elegant way to render names without a prefix?

+2


source to share


1 answer


You can use proper overloading :



<%: Html.EditorFor(x => x.Search, "SearchViewModel", "") %>

      

+6


source







All Articles