对象模板 - 使用 ViewBag 的 Html.Display
我有以下对象显示模板(source):
@model object
@if (Model == null) {
@ViewData.ModelMetadata.NullDisplayText
} else if (ViewData.TemplateInfo.TemplateDepth > 1) {
@ViewData.ModelMetadata.SimpleDisplayText
} else {
foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm))) {
if (prop.HideSurroundingHtml) {
@Html.Display(prop.PropertyName)
} else {
<div>
<strong>@prop.GetDisplayName():</strong> @Html.Display(prop.PropertyName)
</div>
}
}
}
效果很好,直到 ViewBag
中有相同的键。例如,
public class Article {
public string Title { get; set; }
public string Text { get; set; }
}
应该显示以下类(并且将显示空 ViewBag),
Title: Awesome title
Text: Awesome text
但是当 ViewBag.Title
设置为 "Detail"
时,它会显示以下内容,
Title: Detail
Text: Awesome text
这是由 @ 引起的Html.Display(prop.PropertyName)
似乎更喜欢 ViewBag
值而不是模型值。有什么建议吗?我无法使用 Html.DisplayFor
因为我只将属性名称存储在字符串变量中,而不是作为给定模型的表达式。
我想我可以清除 viewbag 并稍后恢复它,但这对我来说看起来不是一个好的解决方案。
编辑:视图看起来像这样:
@model Article
@{
ViewBag.Title = "Detail";
}
@Html.DisplayForModel()
I've got following display template for object (source):
@model object
@if (Model == null) {
@ViewData.ModelMetadata.NullDisplayText
} else if (ViewData.TemplateInfo.TemplateDepth > 1) {
@ViewData.ModelMetadata.SimpleDisplayText
} else {
foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm))) {
if (prop.HideSurroundingHtml) {
@Html.Display(prop.PropertyName)
} else {
<div>
<strong>@prop.GetDisplayName():</strong> @Html.Display(prop.PropertyName)
</div>
}
}
}
Which works great, until there is same key in ViewBag
. For example following class
public class Article {
public string Title { get; set; }
public string Text { get; set; }
}
should display (and will display with empty ViewBag)
Title: Awesome title
Text: Awesome text
however it displays following when ViewBag.Title
is set to "Detail"
Title: Detail
Text: Awesome text
It is caused by @Html.Display(prop.PropertyName)
that seem to prefer ViewBag
value against model value. Any suggestions? I can't use Html.DisplayFor
because I have only property name stored in string variable, not as expression for given model.
I guess I could clear viewbag and restore it later, but that doesn't look like good solution to me.
Edit: View looks like this:
@model Article
@{
ViewBag.Title = "Detail";
}
@Html.DisplayForModel()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至于 ViewBag.Title 本身,您可以将分配移到页面末尾:
对于其他 ViewBag 项目,您可以使用不常见的项目名称(有几个前导)下划线等)
As for ViewBag.Title itself you could move assignment to it at the end of a page:
As for other ViewBag items you could use item names which are uncommon (with several leading underscores etc.)
找到解决方案:http://haacked。 com/archive/2010/05/05/asp-net-mvc-tabular-display-template.aspx -
@Html.DisplayFor(m => prop.Model)
的工作方式为预期的。谁知道元数据中有Model
属性..Found solution: http://haacked.com/archive/2010/05/05/asp-net-mvc-tabular-display-template.aspx -
@Html.DisplayFor(m => prop.Model)
works as expected. Who knew there wasModel
property in metadata..