如何获得查看HTML并返回到客户端 [英] How to get view html and return to client side
本文介绍了如何获得查看HTML并返回到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是code片段它会返回以jQuery函数,但我想知道我怎么能提取或获取查看HTML并返回到客户端;
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>
和Foo操作里面,你可以返回一个局部视图:
and inside the Foo action you could return a partial view:
public ActionResult Foo(string value)
{
SomeModel model = ...
return PartialView(model);
}
在Web表单这样我extarct的用户控件或任何控件相关的HTML。
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();
因此,如何做同样的MVC中。只是想知道我怎么能获得操作方法的查看HTML。谢谢
so how to do the same in mvc. just like to know how could i get the view html from action method. thanks
推荐答案
您可以使用此方法,从控制器传递的ActionResult,并从视图中取回HTML
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();
}
这篇关于如何获得查看HTML并返回到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文