使用带有操作参数的 ASP.NET MVC 子控制器?
我正在尝试 MvcContrib 子控制器。查看源代码中的示例,您的父控制器 (HomeController) 执行一个将子控制器 (FirstLevelSubController) 作为参数的操作:
public class HomeController : Controller
{
public ActionResult Index(FirstLevelSubController firstLevel)
{
ViewData["Title"] = "Home Page";
return View();
}
}
Home 的索引视图中,您可以像这样调用 ViewData.Get 来呈现子控制器及其视图:
<div style="border:dotted 1px blue">
<%=ViewData["text"] %>
<% ViewData.Get<Action>("firstLevel").Invoke(); %>
</div>
在 操作被调用(忽略第二级控制器,该示例只是演示如何嵌套多个子控制器):
public class FirstLevelSubController : SubController
{
public ViewResult FirstLevel(SecondLevelSubController secondLevel)
{
ViewData["text"] = "I am a first level controller";
return View();
}
}
这一切都有效,子控制器的视图在父视图内呈现。
但是,如果我的家庭控制器的操作中需要其他参数怎么办?例如,我可能想将 Guid 传递给控制器的索引方法:
public class HomeController : Controller
{
public ActionResult Index(Guid someId, FirstLevelSubController firstLevel)
{
//Do something with someId
ViewData["Title"] = "Home Page";
return View();
}
}
似乎没有任何方法可以执行 <% ViewData.Get("firstLevel").Invoke(); %>带参数。所以我不知道如何从另一个控制器链接到我的控制器,传递这样的参数:
Html.ActionLink<HomeController>(x => x.Index(someThing.Id), "Edit")
也许我正在以错误的方式处理这个问题?我还能如何让我的父控制器使用子控制器,同时还能做一些有趣的事情,比如接受参数/参数?
I am experimenting with MvcContrib subcontrollers. Looking at the example in the source, your parent controller (HomeController) takes an action which takes the subcontroller (FirstLevelSubController) as a parameter:
public class HomeController : Controller
{
public ActionResult Index(FirstLevelSubController firstLevel)
{
ViewData["Title"] = "Home Page";
return View();
}
}
In Home's index view, you call ViewData.Get like this to render the subcontroller and it's view:
<div style="border:dotted 1px blue">
<%=ViewData["text"] %>
<% ViewData.Get<Action>("firstLevel").Invoke(); %>
</div>
The subcontroller's action gets called (ignore the secondlevelcontroller, the example is just demonstrating how you can nest multiple subcontrollers):
public class FirstLevelSubController : SubController
{
public ViewResult FirstLevel(SecondLevelSubController secondLevel)
{
ViewData["text"] = "I am a first level controller";
return View();
}
}
This all works, the subcontroller's view gets rendered inside the parent view.
But what if I need other parameters in my home controller's action? For example, I may want to pass a Guid to my controller's index method:
public class HomeController : Controller
{
public ActionResult Index(Guid someId, FirstLevelSubController firstLevel)
{
//Do something with someId
ViewData["Title"] = "Home Page";
return View();
}
}
There doesn't seem to be any way to do <% ViewData.Get("firstLevel").Invoke(); %> with parameters. So I can't figure out how to link to my controller from another controller passing a parameter like this:
Html.ActionLink<HomeController>(x => x.Index(someThing.Id), "Edit")
Perhaps I am approaching this the wrong way? How else can I get my parent controller to use a subcontroller, but also do interesting stuff like accept parameters / arguments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下这篇博文:
将对象传递给子控制器
http://mhinze.com/ pass-objects-to-subcontrollers/
请注意,子控制器已被弃用。它们已被
RenderAction
取代。Have a look at this blog post:
Passing objects to SubControllers
http://mhinze.com/passing-objects-to-subcontrollers/
Note that SubControllers are deprecated. They have been replaced with
RenderAction
.