带有 UIHint 的 ASP.NET MVC 3 自定义显示模板 - 需要 For 循环吗?
如果我有一个像这样的 ViewModel:
public class MyViewModel
{
[UIHint("SomeTemplate")]
public ICollection<SomeViewModel> Submodel { get; set; }
}
和一个带有这样一行 HTML 的强类型 View:
@Html.DisplayFor(model => model.Submodel)
以及一个带有签名的 显示模板像这样:
@model MvcApplication1.Models.SomeViewModel
我收到一条错误消息“模型项的类型为 List
但该字典需要 SomeViewModel
类型的模型。”。
这是有道理的,但我希望 MVC 的内置模板智能能够发挥作用,看到它是一个 IEnumerable 的东西,并计算出调用我的模板 N 次,就像通常那样对于没有提示的 Html.DisplayFor
来说,确实如此。
所以看起来[UIHint]
覆盖了该功能?
显然我可以指向另一个接受集合的模板,并调用Html.DisplayForModel()
,基本上模仿 MVC 智能。但我希望避免这种情况。老实说,我宁愿做一个 foreach 循环,也不愿使用 1 行“包装器”模板。
还有更好的想法吗?
就像我想说:“嘿 MVC,为每个人渲染一个模板。但是,不要使用名称约定来查找模板,这里有一个提示”。
If i have a ViewModel like this:
public class MyViewModel
{
[UIHint("SomeTemplate")]
public ICollection<SomeViewModel> Submodel { get; set; }
}
And a strongly-typed View with a line of HTML like this:
@Html.DisplayFor(model => model.Submodel)
And a display template with a signature like this:
@model MvcApplication1.Models.SomeViewModel
I get an error saying "the model item is of type List<SomeViewModel>
but this dictionary requires a model of type SomeViewModel
.".
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.
So it looks like [UIHint]
overrides that functionality?
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.
Any better ideas?
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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UIHint 的意思是“使用名为 XXX 的模板渲染此模型”。因此,您必须声明您的显示模板“SomeTemplate”
并在 foreach 中显示每个项目。
UIHint means "Render this model using the template named XXX". So you have to declare your displaytemplate "SomeTemplate" with
And display each item inside a foreach.
另一种方法是传递字符串模板名称,如下所示
An alternative is to pass the string template name as follow
我遇到了同样的问题。看起来复杂类型默认会忽略 UIHint。您可以覆盖该行为,但这并不简单。所以更简单的解决方案是:
1)删除 UIHint 注释。
2) 相反,请确保您的显示模板文件被命名为您希望 Html.DisplayFor 自动迭代的类型名称。因此,在您的情况下,将显示模板文件命名为 SomeViewModel.cshtml。这应该有效。无需显式使用 for 循环。我已经在MVC4中尝试过并且有效。
我从以下链接得到了解决方案:
http://roysvork.wordpress.com/2012/12/09/dynamic-repeating-field-groups-in-asp-net-mvc-with-just-a-dash-of-淘汰赛-js/
I ran into the same problem. It looks like UIHint is ignored by default for complex types. You can override the behavior but it is not straightforward. So the simpler solution would be:
1) Remove the UIHint annotation.
2) Instead make sure your display template file is named as the type name you want the Html.DisplayFor to automatically iterate over. So in your case, name the display template file as SomeViewModel.cshtml. This should work. There is no need to explicitly use the for loop. I have tried it in MVC4 and it works.
I got the solution from the following link:
http://roysvork.wordpress.com/2012/12/09/dynamic-repeating-field-groups-in-asp-net-mvc-with-just-a-dash-of-knockout-js/