花了一些时间,但我找到了一个很好的解决方案。 Keith 的解决方案适用于很多人,但在某些情况下它并不是最好的,因为有时您希望您的应用程序通过控制器的进程 来呈现视图,而 Keith 的解决方案只是用给定的模型渲染视图我在这里提出一个新的解决方案,它将运行正常的过程。
一般步骤:
- 创建实用程序类
- 使用虚拟视图创建虚拟控制器
- 在您的
aspx 或master page 中,调用实用程序方法来渲染部分传递控制器、视图以及要渲染的模型(作为对象),
让我们在这个例子中仔细检查一下
1) 创建一个名为MVCUtility 的类并创建以下方法:
//Render a partial view, like Keith's solution
private static void RenderPartial(string partialViewName, object model)
{
HttpContextBase httpContextBase = new HttpContextWrapper(HttpContext.Current);
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Dummy");
ControllerContext controllerContext = new ControllerContext(new RequestContext(httpContextBase, routeData), new DummyController());
IView view = FindPartialView(controllerContext, partialViewName);
ViewContext viewContext = new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), httpContextBase.Response.Output);
view.Render(viewContext, httpContextBase.Response.Output);
}
//Find the view, if not throw an exception
private static IView FindPartialView(ControllerContext controllerContext, string partialViewName)
{
ViewEngineResult result = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName);
if (result.View != null)
{
return result.View;
}
StringBuilder locationsText = new StringBuilder();
foreach (string location in result.SearchedLocations)
{
locationsText.AppendLine();
locationsText.Append(location);
}
throw new InvalidOperationException(String.Format("Partial view {0} not found. Locations Searched: {1}", partialViewName, locationsText));
}
//Here the method that will be called from MasterPage or Aspx
public static void RenderAction(string controllerName, string actionName, object routeValues)
{
RenderPartial("PartialRender", new RenderActionViewModel() { ControllerName = controllerName, ActionName = actionName, RouteValues = routeValues });
}
创建一个传递参数的类,我这里调用RendeActionViewModel(可以在MvcUtility Class的同一个文件中创建)
public class RenderActionViewModel
{
public string ControllerName { get; set; }
public string ActionName { get; set; }
public object RouteValues { get; set; }
}
2) 现在创建一个名为DummyController的控制器
//Here the Dummy controller with Dummy view
public class DummyController : Controller
{
public ActionResult PartialRender()
{
return PartialView();
}
}
使用以下内容为DummyController 创建一个名为PartialRender.cshtml(剃刀视图)的虚拟视图,注意它将使用 Html 帮助程序执行另一个渲染操作。
@model Portal.MVC.MvcUtility.RenderActionViewModel
@{Html.RenderAction(Model.ActionName, Model.ControllerName, Model.RouteValues);}
3) 现在只需将其放入您的MasterPage 或aspx 文件中,即可部分渲染您想要的视图。请注意,当您有多个 Razor 视图想要与 MasterPage 或 aspx 页面混合时,这是一个很好的答案。 (假设我们有一个名为 Login 的 PartialView 用于 Controller Home)。
<% MyApplication.MvcUtility.RenderAction("Home", "Login", new { }); %>
或者如果你有一个模型来传递给 Action
<% MyApplication.MvcUtility.RenderAction("Home", "Login", new { Name="Daniel", Age = 30 }); %>
这个解决方案很棒,不使用ajax调用,不会导致嵌套视图延迟渲染,它不会产生new WebRequest 所以它不会给你带来一个新的会话,它会为你想要的视图处理获取ActionResult的方法,它无需通过任何模型即可工作
感谢 Using MVC RenderAction within a Webform