Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a Partial View's HTML from inside of the controller

I have developed a simple mechanism for my mvc website to pull in html via jquery which then populates a specified div. All is well and it looks cool.
My problem is that i'm now creating html markup inside of my controller (Which is very easy to do in VB.net btw) I'd rather not mix up the sepparation of concerns.

Is it possible to use a custom 'MVC View User Control' to suit this need? Can I create an instance of a control, pass in the model data and render to html? It would then be a simple matter of rendering and passing back to the calling browser.

like image 374
Andrew Harry Avatar asked Nov 13 '08 02:11

Andrew Harry


People also ask

How do you return a partial view from controller?

In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.

How do you call a partial view from a webpage or controller?

To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.

How do I render a partial view inside?

Partial function which renders the Partial View. The name of the View and the object of the CustomerModel class are passed to the @Html. Partial function. In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.

Can we return partial view from action method?

To return a Partial view from the controller action method, we can write return type as PartialViewResult and return using PartialView method.


2 Answers

This is a solution that is working with ASP.Net MVC 1.0 (many that claim to work with beta 3 don't work with 1.0), doesn't suffer of the 'Server cannot set content type after HTTP headers have been sent' problem and can be called from within a controller (not only a view):

/// <summary> /// Render a view into a string. It's a hack, it may fail badly. /// </summary> /// <param name="name">Name of the view, that is, its path.</param> /// <param name="data">Data to pass to the view, a model or something like that.</param> /// <returns>A string with the (HTML of) view.</returns> public static string RenderPartialToString(string controlName, object viewData) {     ViewPage viewPage = new ViewPage() { ViewContext = new ViewContext() };     viewPage.Url = GetBogusUrlHelper();      viewPage.ViewData = new ViewDataDictionary(viewData);     viewPage.Controls.Add(viewPage.LoadControl(controlName));      StringBuilder sb = new StringBuilder();     using (StringWriter sw = new StringWriter(sb)) {         using (HtmlTextWriter tw = new HtmlTextWriter(sw)) {             viewPage.RenderControl(tw);         }     }      return sb.ToString(); }  public static UrlHelper GetBogusUrlHelper() {   var httpContext = HttpContext.Current;    if (httpContext == null) {     var request = new HttpRequest("/", Config.Url.ToString(), "");     var response = new HttpResponse(new StringWriter());     httpContext = new HttpContext(request, response);   }    var httpContextBase = new HttpContextWrapper(httpContext);   var routeData = new RouteData();   var requestContext = new RequestContext(httpContextBase, routeData);    return new UrlHelper(requestContext); } 

It's a static method you can drop somewhere you find it convenient. You can call it this way:

string view = RenderPartialToString("~/Views/Controller/AView.ascx", someModelObject);  
like image 57
pupeno Avatar answered Oct 04 '22 03:10

pupeno


I put together a rough framework which allows you to render views to a string from a controller method in MVC Beta. This should help solve this limitation for now.

Additionally, I also put together a Rails-like RJS javascript generating framework for MVC Beta.

Check it out at http://www.brightmix.com/blog/how-to-renderpartial-to-string-in-asp-net-mvc and let me know what you think.

like image 36
Kevin Zink Avatar answered Oct 04 '22 02:10

Kevin Zink