4

With the following code:

static string RenderViewToString(ControllerContext context,string viewPath,object model = null,bool partial = false)
{
    // first find the ViewEngine for this view
    ViewEngineResult viewEngineResult = null;
    if (partial)
        viewEngineResult = ViewEngines.Engines.FindPartialView(context, viewPath);
    else
        viewEngineResult = ViewEngines.Engines.FindView(context, viewPath, null);

    if (viewEngineResult == null)
        throw new FileNotFoundException("View cannot be found.");

    // get the view and attach the model to view data
    var view = viewEngineResult.View;
    context.Controller.ViewData.Model = model;

    string result = null;

    using (var sw = new StringWriter())
    {
        var ctx = new ViewContext(context, view,
                                  context.Controller.ViewData,
                                  context.Controller.TempData,
                                  sw);
        view.Render(ctx, sw);
        result = sw.ToString();
    }

    return result;
}

After this line:

viewEngineResult = ViewEngines.Engines.FindPartialView(context, viewPath);

is executed, I find that viewEngineResult is NULL which I believe means the view is not found even though I placed it correctly in the same path.

Craig W.
  • 17,838
  • 6
  • 49
  • 82
Muhammad Usman
  • 1,366
  • 5
  • 18
  • 33
  • 1
    Welcome to Stack Overflow. I have updated the title of your question to the actual relevant problem you are having. It doesn't really matter what you are doing with the view, the actual problem trying to be solved (unless I am mistaken) is that your view is not found. – Erik Philips Apr 11 '14 at 15:17
  • Thanks .... yes you are right view is not found but i give the exact path.. :( – Muhammad Usman Apr 11 '14 at 15:25
  • 1
    What do you mean by "place it ... in the same path"? IIRC, FindPartialView uses a "by convention" search that will look in the Views subfolder for the controller, then in Shared. What you pass to the call is not the path to the view but simply the view name. – Craig W. Apr 11 '14 at 17:26
  • Thanks Craig W... yes i am passong just view name – Muhammad Usman Apr 11 '14 at 17:30
  • Could you post an example of how you are using this function? – Jasen Apr 11 '14 at 17:42

2 Answers2

1

I'm not sure why your code is returning null, but this works for me in the context of the controller:

    public string RenderRazorViewToString(string viewName, object model = null)
    {
        ViewData.Model = model;
        using (var sw = new System.IO.StringWriter())
        {
            var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);
            return sw.GetStringBuilder().ToString();
        }
    }

Usage:

    var result = RenderRazorViewToString("_GridHeaderStudentCell");

You could pretty easily modify this to take ControllerContext in as a parameter, though.

jdehlin
  • 10,909
  • 3
  • 22
  • 33
-1

maybe could't find your view, make sure your view placed in a folder with same name, something like this find 'logs' view:

  • Views folder
    • Logs folder
      • logs.cshtml file
saiedkia
  • 69
  • 1
  • 4