1

Following is my Action

 public PartialViewResult PrintOrder(string orderNo)
        {
            ViewBag.OrderId = orderNo;
            return PartialView();
        }

Following is my code

public static String RenderViewToString(PartialViewResult result,ControllerContext context, String viewPath,string orderNo, object model = null)
        {

            context.Controller.ViewData.Model = model;
                using (var sw = new StringWriter())
                {
                    result.View = ViewEngines.Engines.FindPartialView(context, "PrintOrder").View;

                    ViewContext vc = new ViewContext(context, result.View, result.ViewData, result.TempData, sw);

                    result.View.Render(vc, sw);

                    var html = sw.GetStringBuilder().ToString();

                    return html;
            }

                //return "No Data";
        }

I am getting an error at result.View.Render(vc, sw) as "Value does not fall within the expected range."

what is the error ?

Aditya Pewekar
  • 91
  • 3
  • 14

1 Answers1

-1

If required to convert your partial view to html string and return the string from the controller try this code.

Public ActionResult PrintOrder(string orderNo) 
{
//code to get the model
this.ViewData.Model = YourModel;
//ViewData["ordno"] = orderNo; uncomment this if you need 
var htmlStr = string.Empty;
try {
    var sw = new StringWriter();
    var viewResult = ViewEngines.Engines.FindPartialView(this.ControllerContext, "partialviewName");
    var viewContext = new ViewContext(this.ControllerContext, viewResult.View, this.ViewData, this.TempData, sw);
    viewResult.View.Render(viewContext, sw);
    viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
    htmlStr = sw.GetStringBuilder().ToString();
}catch(Exception e){
}

return htmlStr;
}
BruceWayne
  • 55
  • 5
  • What does this have to do with the question? And the code is just a repeat of OP's code! –  Oct 26 '16 at 10:35