ASP.NET MVC 3 Razor DisplayFor委托 [英] ASP.NET MVC 3 Razor DisplayFor Delegate

查看:95
本文介绍了ASP.NET MVC 3 Razor DisplayFor委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

模板只能与字段访问,属性访问,一维数组索引或单参数自定义索引器表达式一起使用.

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

这是我的代码(自定义HTML助手,包装DisplayFor,以便我可以选择模板):

Here's my code (custom HTML helper, wrapping DisplayFor so i can choose a template):

public static string DisplayLocationTypeFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, LocationType>> expression, bool plural = false)
{
   return plural ? 
      htmlHelper.DisplayFor(expression, "LocationTypePlural").ToHtmlString() :
      htmlHelper.DisplayFor(expression).ToHtmlString();
}

当我这样使用它时,它会起作用:

When i use it like this, it works:

@Html.DisplayLocationTypeFor(model => model.LocationType)

因为model具有LocationType的属性.

但是当我在另一个自定义HTML帮助程序中执行此操作时:

But when i do this in another custom HTML helper:

public static MvcHtmlString SearchPreferenceButtonForModel<TModel>(this HtmlHelper<TModel> htmlHelper)
{
   // .. other code
   foreach (var property in htmlHelper.ViewData.ModelMetadata.Properties)
   {
      if (property.PropertyName == "LocationType")
         htmlHelper.DisplayLocationTypeFor(model => ((LocationType)Enum.ToObject(typeof(LocationType), property.Model)), true);
   } 
}

它出错.

我可以将DisplayLocationTypeFor助手更改为使用htmlHelper.Display,但是我不确定如何使用.

I can change my DisplayLocationTypeFor helper to use htmlHelper.Display instead, but i'm not sure how.

有什么想法吗?

我正在尝试做的事情,是我有一种具体的方式来呈现LocationType模型,我想在整个网站上进行这种工作.在内部,模板使用资源文件以及基于URL的其他一些smart.换句话说,这里有逻辑-我不想重复.

What i'm trying to do, is that i have a specific way of rendering out the LocationType model, that i want to happen across the site. Internally, the template uses a resource file, and some other smarts based on the URL. In other words, there is logic - which i don't wanted repeated.

通过这种方式,我所有的视图/模板都将调用此模板作为呈现LocationType的标准方法.

This way, all my views/templates call into this template as a standard way of rendering the LocationType.

推荐答案

您需要阅读错误消息:

模板只能与字段访问,属性访问,一维数组索引或单参数自定义索引器表达式一起使用.

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

这告诉您Razor模板中仅允许某些类型的(非常简单!)lambda表达式.如果您有更复杂的东西,则需要先计算值,然后再尝试将其传递给模板.这样的事情应该起作用:

It's telling you that only certain types of (very simple!) lambda expressions are permitted in a Razor template. If you have something more complex, you need to compute the value before you try to pass it to the template. Something like this should work:

if (property.PropertyName == "LocationType") {
  LocationType locationType = (LocationType) Enum.ToObject(typeof(LocationType), property.Model));
  htmlHelper.DisplayLocationTypeFor(model => locationType, true);
} 

这篇关于ASP.NET MVC 3 Razor DisplayFor委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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