Asp.net MVC 2模板没有preFIX [英] Asp.net mvc 2 templates without prefix

查看:108
本文介绍了Asp.net MVC 2模板没有preFIX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下视图模型:

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

我使用编辑模板标题浏览模式:

I'm using editor template for Header view model:

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

编辑模板(EditorTemplates / HeaderViewModel.ascx)

The editor template (EditorTemplates/HeaderViewModel.ascx)

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

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

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

结果:

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

如果我删除行

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

结果是:

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

有另一种方式来实现相同的 - 呈现控件的名称,而不preFIX

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

我在想一个帮手:

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

  return htmlHelper.EditorForModel();
}

和使用它:

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

但它抛出异常。

或者,也许你知道另一种优雅的方式来呈现,而不preFIX名字?

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

推荐答案

您可以使用适当超载

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

这篇关于Asp.net MVC 2模板没有preFIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆