对象模板 - 使用 ViewBag 的 Html.Display

发布于 2024-10-17 20:21:06 字数 1543 浏览 1 评论 0原文

我有以下对象显示模板(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无言温柔 2024-10-24 20:21:06

至于 ViewBag.Title 本身,您可以将分配移到页面末尾:

@model Article

@Html.DisplayForModel()

@{
    ViewBag.Title = "Detail";
}

对于其他 ViewBag 项目,您可以使用不常见的项目名称(有几个前导)下划线等)

As for ViewBag.Title itself you could move assignment to it at the end of a page:

@model Article

@Html.DisplayForModel()

@{
    ViewBag.Title = "Detail";
}

As for other ViewBag items you could use item names which are uncommon (with several leading underscores etc.)

药祭#氼 2024-10-24 20:21:06

找到解决方案: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 was Model property in metadata..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文