ASP.Net MVC, RenderPartial to string with ViewModel data
I am attempting to render a partial view to a string from within a controller. I am using the following which is working well:
StringBuilder ViewBuilder = new StringBuilder();
using (StringWriter ViewWriter = new StringWriter(ViewBuilder))
{
ViewEngineResult ViewResult = ViewEngines.Engines.FindPartialView(PageController.ControllerContext, controlName);
ViewContext ViewContext = new ViewContext(PageController.ControllerContext, ViewResult.View, PageController.ViewData, PageController.TempData, ViewWriter);
ViewResult.View.Render(ViewContext, ViewWriter);
}
ret开发者_如何学编程urn ViewBuilder.ToString();
Reference: http://craftycodeblog.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/
However I would like to be able to load typed model data into this view as well. I am looking for ideas on how this might be done.
Thanks
Edit:
It turns out I was looking in the wrong spot. I was able to set it using the ViewDataDictionary, see below:
ViewDataDictionary ViewData = new ViewDataDictionary();
ViewData.Model = GetModelState();
I think Model is a property of View or ViewContext.
Have a look at this. http://blog.learningdds.com/?p=87
It is an extension method for the controller that renders the HTML in a View / PartialView to a String.
The code for it is here: http://learningdds.com/public/ControllerExtension.cs
It is based on the blog you refered to above.
精彩评论