使用ViewContext.Writer的HtmlHelper无法正确呈现 [英] HtmlHelper using ViewContext.Writer not rendering correctly

查看:80
本文介绍了使用ViewContext.Writer的HtmlHelper无法正确呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用asp.net MVC构建一个简单的CMS,并且几乎所有部分都在工作.我有一个小问题,我无法解决.我有一个HTML辅助方法,可以呈现区域的内容.但是,此方法使用Response.Write写入其内容,而不是返回字符串.这样做的原因是我正在执行部分请求,因此没有要返回的字符串.

I am building a simple CMS with asp.net MVC and I have almost all the parts working. I have one minor issue which I can't work out how to solve. I have a Html helper method that renders the content of area. This method however uses the Response.Write to write its content rather than returning a string. The reason for this is that I'm performing a partial request and hence do not have a string to return.

我的模板正文如下:

<body>
    <h1>Default Template</h1>
    <% Html.ContentArea("Main"); %>
</body>

我遇到的问题是内容呈现在H1标签上方.我的理解是h1应该已经在响应中,因此我对response.Write()的调用将在此点之后附加内容.这显然是没有发生的.我在这里想念什么.

The problem I'm having is that the content renders above the H1 tag. My understand was that the h1 should already be in the response and my call to response.Write() would therefore append content after that point. This is obviously not happening. What am I missing here.

我已经使用部分请求之前,它们始终呈现在正确的位置.我尚未将它们与MVC 2一起使用,这可能是问题所在.

I've used partial requests before and they always render in the correct place. I haven't yet used them with MVC 2, this could possibly be the problem.

下面是内容区域扩展方法.窗口小部件的渲染方法各不相同,这是发生部分请求的地方.但是,即使起始和结束容器标签都呈现在h1以上,所以它必须是我做错的基本操作.

Below is the content area extension method. The widget render method varies and is where the partial request occurs. However even the start and end container tag render above h1 so it has to be something fundamental I'm doing wrong.

    public static void ContentArea(this HtmlHelper htmlHelper, string areaName)
    {
        var container = new TagBuilder("div");
        container.GenerateId(areaName);
        container.AddCssClass("content-area");

        var response = htmlHelper.ViewContext.HttpContext.Response;

        response.Write(container.ToString(TagRenderMode.StartTag));

        var pageWidgets = htmlHelper.ViewData[areaName] as IList<PageWidget>;
        if (pageWidgets != null)
            foreach (var widget in pageWidgets)
            {
                widget.GetInstance().Render(new WidgetContext(htmlHelper.ViewContext, widget));
            }

        response.Write(container.ToString(TagRenderMode.EndTag));
    }

可能很简单……不是总是这样:)

It's probably something simple... Isn't it always :)

即使以下内容不能正确呈现,也不能使用以前的注释使用ViewContext.Writer来代替.

Even the below doesn't render correctly taking previous comments to use ViewContext.Writer instead makes no difference.

    public static void ContentArea(this HtmlHelper htmlHelper, string areaName)
    {
        var container = new TagBuilder("div");
        container.GenerateId(areaName);
        container.AddCssClass("content-area");

        var writer = htmlHelper.ViewContext.Writer;

        writer.Write(container.ToString(TagRenderMode.StartTag));

        writer.Write(container.ToString(TagRenderMode.EndTag));
    }

这呈现为

<div id="Main" class="content-area"></div>
<h1>Default Template</h1>

谢谢

伊恩

推荐答案

您是否尝试过使用ViewContext.Writer.Write而不是Response.Write?我认为这就是我在扩展方法中使用的方法,并且没有任何问题.

Have you tried using ViewContext.Writer.Write rather than Response.Write? I think that's what I use in my extension methods, and I haven't had any issues.

这篇关于使用ViewContext.Writer的HtmlHelper无法正确呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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