Question

I have a partial view (Company.ascx) in the DisplayTemaplates folder. I can render its content in another view using following structure <% Html.DisplayFor(m => m.Company) %>.

Can I render and somehow get the Html string from within a controller? I need that because I'm using Ajax which have to return html (Company structure) as a result.

Was it helpful?

Solution

If your controller action needs to return the HTML result of the execution of this display template you could simply indicate the path to this template and pass the required model:

public ActionResult SomeAjaxAction() 
{
    var company = FetchCompanyFromSomewhere();
    return View("~/Views/Home/DisplayTemplates/Company.ascx", company);
}

As far as rendering a view to a HTML string is concerned there are some possibilities but I wouldn't recommend you doing it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top