如何在ASP中使用Html.Action()将参数传递给操作。净MVC吗?

时间:2021-08-17 07:17:56

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.

我一直使用Html。动作(“ActionName”、“ControllerName”)跨控制器调用子动作,而不需要在视图*享视图。这对于显示会话或cookie信息非常有用。

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.

除了访问cookie,我还希望向Html传递其他参数。动作(“ActionName”、“ControllerName”),这样动作就可以根据传递给原始视图的数据执行不同的代码。

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

是否应该使用不同的方法将参数传递给不同控制器中的子操作?一个人如何做到这一点?

2 个解决方案

#1


84  

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

您可以在RouteValues属性中指定其他数据,如下所示。

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

#2


-1  

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

为了对这个问题补充一点,我正在使用ASP。Net MVC 5和我可以通过这段代码成功实现这一点:

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

#1


84  

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

您可以在RouteValues属性中指定其他数据,如下所示。

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

#2


-1  

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

为了对这个问题补充一点,我正在使用ASP。Net MVC 5和我可以通过这段代码成功实现这一点:

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