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() {
$('#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 could return a partial view:
public ActionResult Foo(string value)
{
SomeModel model = ...
return PartialView(model);
}
in web form this way i extarct the usercontrols or any controls related html.
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 like to know how could i get the view html from action method. thanks
You can use this method , passing the ActionResult from controller and getting back 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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With