返回剃须刀使用JSON(ASP MVC 3)局部视图 [英] Return Razor partial view using JSON (ASP MVC 3)

查看:95
本文介绍了返回剃须刀使用JSON(ASP MVC 3)局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC 2与普通视图引擎,我可以通过返回一个ASCX局部视图作为字符串返回JSON()

但随着新的Razor .cshtml观点我无法弄清楚如何做到这一点。我不断收到键入ASP.CustomerForm_cshtml不从'System.Web.UI.UserControl'继承。

局部视图从 System.Web.Mvc.WebViewPage&LT继承; T> 和其他错误弹出,如果我从的System.Web继承。 UI.UserControl< T> 作为第一个错误说:

这是如何解决这一问题使用ASP MVC 3的Razor视图引擎?有什么想法

这是我的ControlToString功能:

 私人字符串ControlToString(串了ControlPath,对象模型)
    {
        的ViewPage页=新的ViewPage();
        ViewUserControl CTL =(ViewUserControl)page.LoadControl(了ControlPath);
        page.Controls.Add(CTL);
        page.ViewData.Model =模型;
        page.ViewContext =新ViewContext();
        System.IO.StringWriter作家=新System.IO.StringWriter();
        System.Web.HttpContext.Current.Server.Execute(页,作家,FALSE);
        字符串outputToReturn = writer.ToString();
        writer.Close();
        //返回this.Json(outputToReturn.Trim());
        返回outputToReturn.Trim();
    }


解决方案

此的可能的帮助你,因为我已经写它关闭我的头顶部没有测试它除了以验证它返回渲染CSHTML文件。

我以为这是在一个控制器的方法。

 私人字符串ControlToString(串了ControlPath,对象模型){
    CshtmlView控制=新CshtmlView(了ControlPath);    HtmlTextWriter的作家=新的HtmlTextWriter(新System.IO.StringWriter());
    control.Render(新ViewContext(this.ControllerContext,控制,this.ViewData,this.TempData,作家),作家);    字符串值=((StringWriter的)writer.InnerWriter)的ToString();    返回值;
}

In MVC 2 with the regular view engine i could return a ascx partial view as string through return Json()

But with the new Razor .cshtml views I can not figure out how to do this. I keep getting Type 'ASP.CustomerForm_cshtml' does not inherit from 'System.Web.UI.UserControl'.

The partial view inherits from System.Web.Mvc.WebViewPage<T> and another error pops up if I inherit from System.Web.UI.UserControl<T> as the first error says.

Any thoughts on how to fix this using ASP MVC 3 and Razor view engine?

This is my ControlToString function:

    private string ControlToString(string controlPath, object model)
    {
        ViewPage page = new ViewPage();
        ViewUserControl ctl = (ViewUserControl)page.LoadControl(controlPath);
        page.Controls.Add(ctl);
        page.ViewData.Model = model;
        page.ViewContext = new ViewContext();
        System.IO.StringWriter writer = new System.IO.StringWriter();
        System.Web.HttpContext.Current.Server.Execute(page, writer, false);
        string outputToReturn = writer.ToString();
        writer.Close();
        //return this.Json(outputToReturn.Trim());
        return outputToReturn.Trim();
    }

解决方案

This might help you as I've written it off the top of my head without testing it other than to validate it returns the rendered cshtml file.

I assumed this was a method in a Controller.

private string ControlToString(string controlPath, object model) {
    CshtmlView control = new CshtmlView(controlPath);

    HtmlTextWriter writer = new HtmlTextWriter(new System.IO.StringWriter());
    control.Render(new ViewContext(this.ControllerContext, control, this.ViewData, this.TempData, writer), writer);

    string value = ((StringWriter)writer.InnerWriter).ToString();

    return value;
}

这篇关于返回剃须刀使用JSON(ASP MVC 3)局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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