How can I pass parameters to an Action using Html.Action() in ASP.NET MVC?

时光怂恿深爱的人放手 提交于 2019-12-04 14:57:24

问题


I've been using Html.Action("ActionName", "ControllerName") to invoke child actions across controllers without needing to have the view in Views\Shared. This has been working great for displaying things like session or cookie information.

Instead of just accessing cookies, I would like to pass additional parameters to Html.Action("ActionName", "ControllerName") so the action can execute different code based on the the data passed to the original view.

Should I be using a different method to pass parameters to a child action in a different controller? How would one accomplish this?


回答1:


You could specify additional data in the RouteValues property like this.

Html.Action("ActionName","Controller", new { id = 1 })



回答2:


To add a little on this question, I am using ASP.Net MVC 5 and I could succeed to achieve this with this code:

@Html.Action("foo",new {parameter1=1})


来源:https://stackoverflow.com/questions/3150491/how-can-i-pass-parameters-to-an-action-using-html-action-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!