How to get html presentation and return to the client side

below is a piece of code that returns a view of the jquery function, but I like to know how I can extract or get the html view and return to the end of the client.

$(function() { $('#myddl').change(function() { var url = $(this).data('url'); var value = $(this).val(); $('#result').load(url, { value: value }) }); }); <div id="result"></div> 

and inside the Foo action you can return a partial view:

 public ActionResult Foo(string value) { SomeModel model = ... return PartialView(model); } 

in a web form this way I extarct usercontrols or any html management related files.

 System.Web.UI.Page pageHolder = new System.Web.UI.Page(); BBAReman.facebox.FeedBack ctl = (BBAReman.facebox.FeedBack)pageHolder.LoadControl("~/UserControls/FeedBack.ascx"); System.Web.UI.HtmlControls.HtmlForm tempForm = new System.Web.UI.HtmlControls.HtmlForm(); tempForm.Controls.Add(ctl); pageHolder.Controls.Add(tempForm); StringWriter output = new StringWriter(); HttpContext.Current.Server.Execute(pageHolder, output, false); outputToReturn = output.ToString(); 

so how to do the same in mvc. just how to find out how I can get the html view from the action method. thanks

+4
source share
3 answers

You can use this method by passing ActionResult from the controller and returning html from the view

  private string RenderActionResultToString(ActionResult result) { // Create memory writer. var sb = new StringBuilder(); var memWriter = new StringWriter(sb); // Create fake http context to render the view. var fakeResponse = new HttpResponse(memWriter); var fakeContext = new HttpContext(System.Web.HttpContext.Current.Request, fakeResponse); var fakeControllerContext = new ControllerContext( new HttpContextWrapper(fakeContext), this.ControllerContext.RouteData, this.ControllerContext.Controller); var oldContext = System.Web.HttpContext.Current; System.Web.HttpContext.Current = fakeContext; // Render the view. result.ExecuteResult(fakeControllerContext); // Restore old context. System.Web.HttpContext.Current = oldContext; // Flush memory and return output. memWriter.Flush(); return sb.ToString(); } 
+9
source

You can make an AJAX call to the MVC action method, which will return a partial view as HTML. Then you just call the .html jquery function to populate your div. Something like that:

 $(function() { $('#myddl').change(function() { var url = $(this).data('url'); var value = $(this).val(); $.ajax({ type: "POST", url: "@Url.Action("Foo", "Controller")", // replace with your actual controller and action data: JSON.stringify({ value: value }), success: function(result) { $('#result').html(result); } }); }); 
+1
source

I understand that you need a way to return the raw HTML code of your presentation to the client side. If this is what you want, you need to use ViewEngine, which MVC uses to render HTML.

The following is an example:

 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(); } 

The above code finds a partial view and converts it to an HTML string using the MVC viewer.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1497997/


All Articles