在控制器中使用:
ViewBag.aaaa= "******";
string pageContent = Utils.GetViewHtml(this.ControllerContext, "/Views/Shared/PayParameterView.cshtml", null);
具体方法
/// 在控制器内获取指定视图生成后的HTML
/// </summary>
/// <param name="context">当前控制器的上下文</param>
/// <param name="viewName">视图名称</param>
/// <param name="model">视图所需要的参数</param>
/// <returns>视图生成的HTML</returns>
public static string GetViewHtml( ControllerContext context, string viewName, Object param)
{
if (string.IsNullOrEmpty(viewName))
viewName = context.RouteData.GetRequiredString("action");
context.Controller.ViewData.Model = param;
using (var sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(context, viewName);
var viewContext = new ViewContext(context,
viewResult.View,
context.Controller.ViewData,
context.Controller.TempData,
sw);
try
{
viewResult.View.Render(viewContext, sw);
}
catch (Exception ex)
{
throw;
}
return sw.GetStringBuilder().ToString();
}
}
在对应的页面上可以取到控制器中的ViewBag数据