How to get view html and return to client side

后端 未结 3 943
太阳男子
太阳男子 2021-01-06 08:19

below is code snippet which return view to jquery function but i like to know how could i extract or get the view html and return to client end.

$(function(         


        
3条回答
  •  广开言路
    2021-01-06 08:43

    What I understand is you need a way to return raw HTML of your view to client side. If that's what you want you have to use the ViewEngine that MVC uses to render HTML.

    Below is the sample :

    using (StringWriter sw = new StringWriter())
                {
                    ViewEngineResult viewResult = null;
    
                    viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
    
                    var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                    viewResult.View.Render(viewContext, sw);
    
                    return sw.GetStringBuilder().ToString();
                }
    

    Above code finds a partial view and converts it into HTML String by MVC view engine.

    Hope this helps.

提交回复
热议问题