views:

275

answers:

3

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();
    }
+3  A: 

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;
}
BuildStarted
Thank you, it works. I get the view as a string. But the model is not beeing taken care of anywhere in your code. I tried to add the model as a parameter in `CshtmlView()` but it did not take model as a parameter. Any thoughts?
Martin
i worked it out, you have to add `this.ViewData.Model = model;` in your code, then it works :D
Martin
A: 

I'm trying this code out and I can't seem to find CshtmlView, what namespace is that in? I do have System.Web.Mvc 3.0 installed and referenced in my project. Thanks.

Try the code I have pasted in a answer below.
Martin
A: 

This is the MVC 3 Beta version of the code:

    private string ControlToString(string controlPath, object model)
    {
        //CshtmlView control = new CshtmlView(controlPath);
        RazorView control = new RazorView(this.ControllerContext, controlPath, null, false, null);

        this.ViewData.Model = model;

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