سؤال

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.

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top