MVC4剃刀DisplayTemplate调用,生成的HTML,而不是呈现到浏览器 [英] MVC4 Razor DisplayTemplate called, HTML generated, but not rendered to browser

查看:244
本文介绍了MVC4剃刀DisplayTemplate调用,生成的HTML,而不是呈现到浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了一个迭代收集和电话 DisplayFor()为集合中的每个元素的视图。

我需要手动迭代(相对于收集传递到DisplayFor)以便告诉该模板如果列表中休息应绘制。在列表中的项目只会是2种类型,他们订购的,所以我只需要一次显示此休息一下。

我的模板是发现和正确调用。
结果我可以看到它正确生成HTML,例如: DisplayFor()ToHtmlString()
结果我可以设置这个HTML为范围的变量,即: VAR HTML = DisplayFor()ToHtmlString() ..
结果但是,即使 Html.Raw(HTML)不会呈现在浏览器 - HTML是否已杳如黄鹤。

这是怎么回事?

  VAR renderBreakInList = Model.Items.Any(X => x.IsSomeType);
的foreach(在Model.Items VAR项)
{
    VAR renderBreak = renderBreakInList&放大器;&安培; item.IsOtherType;
    Html.DisplayFor(X =>的项目,新的{​​renderBreak = renderBreak});    如果(renderBreak)
    {
        renderBreakInList = FALSE;
    }
}


解决方案

在本身的 Html.DisplayFor 方法不会呈现任何响应仅返回生成的HTML作为一个 MvcHtmlString

为了实际编写所呈现的HTML你需要告诉这个剃刀与使用 @ 标志响应:

  @ Html.DisplayFor(X =>的项目,新的{​​renderBreak = renderBreak})

所以,你的整个code应该是这样的:

  @ {
    VAR renderBreakInList = Model.Items.Any(X => x.IsSomeType);
    的foreach(在Model.Items VAR项)
    {
        VAR renderBreak = renderBreakInList&放大器;&安培; item.IsOtherType;
        @ Html.DisplayFor(X =>的项目,新的{​​renderBreak = renderBreak})        如果(renderBreak)
        {
            renderBreakInList = FALSE;
        }
    }
}

或者你也可以使用 WebPageBase.Write 方法(它被使用 @ 标志时,引擎盖下的所谓):

 写(Html.DisplayFor(X =>的项目,新的{​​renderBreak = renderBreak}));

I've got a view that iterates a collection and calls DisplayFor() for each element in the collection.

I need to manually iterate (as opposed to passing the collection to DisplayFor) in order to tell the template if a break in the list should be drawn. The items in the list will only be of 2 types, ordered by them, so I only need to show this break once.

My template is found and called correctly.
I can see the HTML it generates correctly, ie: DisplayFor().ToHtmlString()
I can set this HTML as a scoped variable, ie: var html = DisplayFor().ToHtmlString() ..
But even Html.Raw(html) does not render it in the browser - the HTML has simply vanished.

What's going on?

var renderBreakInList = Model.Items.Any(x => x.IsSomeType);
foreach(var item in Model.Items)
{
    var renderBreak = renderBreakInList && item.IsOtherType;
    Html.DisplayFor(x => item, new { renderBreak = renderBreak });

    if (renderBreak)
    {
        renderBreakInList = false;
    }
}

解决方案

The Html.DisplayFor method in itself does not render anything to the response just returns the generated HTML as a MvcHtmlString.

In order to actually write the rendered HTML to the response you need to tell this to Razor with using the @ sign:

@Html.DisplayFor(x => item, new { renderBreak = renderBreak })

So your whole code should look like this:

@{
    var renderBreakInList = Model.Items.Any(x => x.IsSomeType);
    foreach(var item in Model.Items)
    {
        var renderBreak = renderBreakInList && item.IsOtherType;
        @Html.DisplayFor(x => item, new { renderBreak = renderBreak })

        if (renderBreak)
        {
            renderBreakInList = false;
        }
    }
}

Or you can use the WebPageBase.Write method (which gets called under the hood when using the @ sign):

Write(Html.DisplayFor(x => item, new { renderBreak = renderBreak }));

这篇关于MVC4剃刀DisplayTemplate调用,生成的HTML,而不是呈现到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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