Sgr*_*ite 0 c# asp.net-mvc asp.net-mvc-2
我正在尝试使用MVC2在VS 2010中使用Html.RenderAction()在我的母版页上呈现局部视图.这是我的RenderAction()调用:
<% Html.RenderAction(
"Menu",
"Navigation",
new
{
currentAction = ViewContext.RouteData.Values["action"],
currentController = ViewContext.RouteData.Values["controller"]
}
); %>
Run Code Online (Sandbox Code Playgroud)
但是,当它是导航控制器的构造函数时,它总是命中没有参数定义的构造函数.
public class NavigationController : Controller
{
public NavigationViewModel navigationViewModel { get; set; }
public NavigationController()
{
-snip-
}
public NavigationController( string currentAction, string currentController )
{
-snip-
}
[ChildActionOnly]
public ViewResult Menu()
{
return View(this.navigationViewModel);
}
}
Run Code Online (Sandbox Code Playgroud)
在我看到的所有示例中,这是使用RenderAction()调用传递参数的方式.如果我删除没有定义参数的构造函数,我不会得到任何错误消息,除了它抱怨.
如何让它调用定义了两个参数的构造函数?我希望能够在构建菜单时与currentAction和currentController进行比较,以正确突出显示用户当前所在的部分.
根据您的示例,您将参数传递给操作,而不是控制器构造函数.
实际上,我认为你应该做的更像是这样
public class NavigationController
{
[ChildActionOnly]
public ViewResult Menu(string currentAction, string currentController)
{
var navigationViewModel = new NavigationViewModel();
// delegates the actual highlighing to your view model
navigationViewModel.Highlight(currentAction, currentController);
return View(navigationViewModel);
}
}
Run Code Online (Sandbox Code Playgroud)