如何在POST上传递Url参数?

时间:2022-06-01 15:15:15

I have a controller method with the following signature:

我有一个带有以下签名的控制器方法:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateValues(int id, MyViewModel[] array)
{
}

The id is normally picked up as part of the Url on other GET controller methods (I have a working route that does this)

id通常作为其他GET控制器方法的Url的一部分被拾取(我有一个工作路由来执行此操作)

I am successfully passing the array1 from the form in my view to the controller method, but how do I also put the id onto my Url so that when the user clicks the Submit button, the controller method will pick up the id?

我成功地将array1从我视图中的表单传递给控制器​​方法,但是我如何将id放到我的Url上,以便当用户单击Submit按钮时,控制器方法将获取id?

1 个解决方案

#1


1  

The id parameter might be set to optional: ASP.NET MVC 2 Optional URL Parameters which is default in ASP.NET MVC 2 so if there is not form element named "id" it will not be passed.

id参数可能设置为optional:ASP.NET MVC 2 ASP.NET MVC 2中默认的可选URL参数,因此如果没有名为“id”的表单元素,则不会传递它。

Just pass the parameter you need as part of the form instead of part of the Url, as in:

只需将您需要的参数作为表单的一部分而不是Url的一部分传递,如:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateValues(int UserID, MyViewModel[] array)
{
}

and in your view:

在你看来:

<%= Html.Hidden("userID", Model.UserID) %>

#1


1  

The id parameter might be set to optional: ASP.NET MVC 2 Optional URL Parameters which is default in ASP.NET MVC 2 so if there is not form element named "id" it will not be passed.

id参数可能设置为optional:ASP.NET MVC 2 ASP.NET MVC 2中默认的可选URL参数,因此如果没有名为“id”的表单元素,则不会传递它。

Just pass the parameter you need as part of the form instead of part of the Url, as in:

只需将您需要的参数作为表单的一部分而不是Url的一部分传递,如:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateValues(int UserID, MyViewModel[] array)
{
}

and in your view:

在你看来:

<%= Html.Hidden("userID", Model.UserID) %>