HtmlHelper using ViewContext.Writer not rendering correctly

梦想的初衷 提交于 2019-12-13 02:47:43

问题


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.

My template body looks like this:

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

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.

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.

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 :)

EDIT:

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));
    }

This renders as

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

Thanks,

Ian


回答1:


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.




回答2:


Apologies for digging up a year-old question, but I've implemented the OP's code and I cannot reproduce this issue on ASP.NET MVC 2.

With this in my View...

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

...I get this output generated:

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


来源:https://stackoverflow.com/questions/3599938/htmlhelper-using-viewcontext-writer-not-rendering-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!