ASP.NET MVC 3自定义显示模板,UIHint - For循环必需? [英] ASP.NET MVC 3 Custom Display Template With UIHint - For Loop Required?
问题描述
如果我有一个视图模型是这样的:
If i have a ViewModel like this:
public class MyViewModel
{
[UIHint("SomeTemplate")]
public ICollection<SomeViewModel> Submodel { get; set; }
}
和一个强类型的查看用这样一行HTML的:
And a strongly-typed View with a line of HTML like this:
@Html.DisplayFor(model => model.Submodel)
和一个显示模板有这样的签名:
@model MvcApplication1.Models.SomeViewModel
我得到一个错误说的模式产品类型的列表与LT; SomeViewModel&GT;
,但本词典需要类型的模型 SomeViewModel
。
I get an error saying "the model item is of type List<SomeViewModel>
but this dictionary requires a model of type SomeViewModel
.".
这是有道理的,但我本来希望MVC的内置模板智慧会踢,看到它是一个的IEnumerable的东西
,并制定叫我的模板施氮量的时候,怎么样它通常为 Html.DisplayFor
确实没有提示。
Which makes sense, but i would have hoped the built-in templating smarts of MVC would kick in, see it's a IEnumerable
of something and work out to call my template N amount of times, like how it usually does for Html.DisplayFor
without the hint.
因此,它看起来像 [UIHint]
将覆盖该功能?
So it looks like [UIHint]
overrides that functionality?
很明显,我可以指向另一个模板,它接受的集合,并调用 Html.DisplayForModel()
,基本上是模仿MVC的智慧。但我希望能避免这种情况。老实说,我宁愿做一个foreach循环比具有1行包装的模板。
Obviously i can point to another template which accepts the collection, and calls Html.DisplayForModel()
, basically emulating MVC smarts. But i am hoping to avoid that. Honestly i would rather do a foreach loop than having that 1 line "wrapper" template.
任何更好的想法?
这就像我想说:嘿MVC中,呈现出这些家伙中的每一个模板,但不是使用名称约定找模板,这里有一个暗示。
It's like i want to say: "Hey MVC, render out a template for each one of these guys. But instead of using name-convention to find the template, here's a hint".
推荐答案
UIHint意思是渲染使用名为XXX模板这种模式。所以,你必须与申报的displaytemplateSomeTemplate
UIHint means "Render this model using the template named XXX". So you have to declare your displaytemplate "SomeTemplate" with
@model MvcApplication1.Models.ICollection<SomeViewModel>
和一个foreach内显示的每个项目。
And display each item inside a foreach.
这篇关于ASP.NET MVC 3自定义显示模板,UIHint - For循环必需?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!