Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将ActionResult呈现为字符串-以错误的顺序出现_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 将ActionResult呈现为字符串-以错误的顺序出现

C# 将ActionResult呈现为字符串-以错误的顺序出现,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我尝试将ActionResult呈现为字符串。我通过传入我自己的HttpContext来实现这一点,它用我自己的TextWriter替换输出文本编写器 问题出在这里——元素的渲染顺序不正确。如果通过浏览器直接查询局部视图来渲染局部视图,则效果良好。如果通过替换的文本编写器进行渲染,则razor视图中的任何@Html.Action元素都会首先进行渲染,而不管它们在视图中的位置如何 下面是我的剃须刀观点: @inherits System.Web.Mvc.WebViewPage<Website

我尝试将
ActionResult
呈现为字符串。我通过传入我自己的
HttpContext
来实现这一点,它用我自己的
TextWriter
替换输出文本编写器

问题出在这里——元素的渲染顺序不正确。如果通过浏览器直接查询局部视图来渲染局部视图,则效果良好。如果通过替换的文本编写器进行渲染,则razor视图中的任何@Html.Action元素都会首先进行渲染,而不管它们在视图中的位置如何

下面是我的剃须刀观点:

@inherits System.Web.Mvc.WebViewPage<WebsitePresentationLayer.MgrScreenLayoutViewer>
@using  System.Web.Mvc.Html;

<div>
    @Model.DebugText
</div> 
@foreach (var item in @Model.Items)
{
    <div>@item.Title</div>
    @Html.Action(
                    "LayoutItem",
                    new
                    {
                        id = item.Id,
                        uniqueName = item.UniqueName
                    }
                  );

}
最后,我使用以下代码将其输出为字符串:

StringBuilder sb = new StringBuilder();
StringWriter stringWriter = new StringWriter(sb);

CMS.Website.MvcUtils.RenderControllerAction<PlayerGroupController>
(
   c => c.ScreenLayout(this.MgrPlayerGroupViewer.ScreenLayoutId),
   stringWriter
);
stringWriter.Flush();
var generatedString = sb.ToString();
StringBuilder sb=新建StringBuilder();
StringWriter StringWriter=新的StringWriter(sb);
CMS.Website.MvcUtils.rendercontrol
(
c=>c.ScreenLayout(this.MgrPlayerGroupViewer.ScreenLayoutId),
编剧
);
stringWriter.Flush();
var generatedString=sb.ToString();

我已经为
TextWriter
编写了一个拦截器,果然,它收到了三个调用
Write(string)

  • 写入(布局项目1动作内容)
  • 写入(布局项目2动作内容)
  • 编写(Model.DebugText和两项标题)

我大约在6个月前经历了这一过程。目标是使用一个partial来填充jquery弹出对话框

问题是视图引擎想要以自己的笨拙顺序渲染它们

试试这个。LMK,如果需要澄清

    public static string RenderPartialViewToString(Controller thisController, string viewName, object model)
    {
        // assign the model of the controller from which this method was called to the instance of the passed controller (a new instance, by the way)
        thisController.ViewData.Model = model;

        // initialize a string builder
        using (StringWriter sw = new StringWriter())
        {
            // find and load the view or partial view, pass it through the controller factory
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(thisController.ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(thisController.ControllerContext, viewResult.View, thisController.ViewData, thisController.TempData, sw);

            // render it
            viewResult.View.Render(viewContext, sw);

            //return the razorized view/partial-view as a string
            return sw.ToString();
        }
    }

谢谢我必须手动创建自己的ControllerContext,但这解决了我的特殊问题。我仍然希望弄清楚为什么调用ActionResult.ExecuteSult时,ViewEngine会以奇怪的顺序渲染。@AndrewShepherd我记得我在这方面做过努力。起初,我只是试图返回一个部分,但没有得到我想要的东西。我必须通过控制器“处理”我的视图。一旦我决定了方向,整天都要先俯首称臣。你似乎在做类似的事情,但方向略有不同。我必须再次深入了解它。
    public static string RenderPartialViewToString(Controller thisController, string viewName, object model)
    {
        // assign the model of the controller from which this method was called to the instance of the passed controller (a new instance, by the way)
        thisController.ViewData.Model = model;

        // initialize a string builder
        using (StringWriter sw = new StringWriter())
        {
            // find and load the view or partial view, pass it through the controller factory
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(thisController.ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(thisController.ControllerContext, viewResult.View, thisController.ViewData, thisController.TempData, sw);

            // render it
            viewResult.View.Render(viewContext, sw);

            //return the razorized view/partial-view as a string
            return sw.ToString();
        }
    }