ASP.NET MVC - 带有来自 URL 的附加参数的 POST 操作方法

发布于 2024-08-25 04:07:04 字数 1026 浏览 13 评论 0原文

使用 ASP.net MVC 是否可以将表单 POST 到控制器操作,其中包含的参数不是表单中的参数,而是来自 URL 的参数?

例如

GroupController 中的 Action 方法:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(int idOne, int idTwo, Model model)
    { ... }

路由:

"{controller}/{action}/{idOne}/{idTwo}"

Posted URL:

/Employee/Show/1/42

在此示例中,表单被发布到不同的控制器,模型具有正确的值,但是其他参数具有默认值 0。

我期望的行为是ModelBinder 会看到我有两个与给定路由匹配的参数,并以与 GET 操作相同的方式将当前值 1 和 42 分配给参数。

是否不支持这种行为,或者我错过了什么?

编辑: 需要明确的是,控制器 EmployeeShow 视图上的表单包含一个发布到不同控制器的表单。我们可以将其称为Group

表单操作 URL 如下

/Groups/Create/0/0

所示 表单声明如下

Html.BeginForm("Create", "Groups")

在尝试了 Html.BeginForm 的许多不同重载之后,我发现仅当表单操作 URL 与浏览器中的当前 URL 匹配时才会映射参数地址栏。

因此,如果我导航到 URL /Groups/Create/1/42 我将得到一个新表单。如果我随后提交表单,URL 路由值将传递到 POST 操作。

With ASP.net MVC is it possible to POST a form to a controller action which includes parameters not in the form, but from the URL?

For example

The Action method in GroupController:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(int idOne, int idTwo, Model model)
    { ... }

The route:

"{controller}/{action}/{idOne}/{idTwo}"

Posted URL:

/Employee/Show/1/42

In this example, the form is being posted to a different controller, the model has the correct value, however the other parameters have default values 0.

The behavior I was expecting is that the ModelBinder would see that I have two parameters that match the given route, and assign the current values of 1 and 42 to the parameters in the same same way a GET operation works.

Is this behavior not supported, or am I missing something?

EDIT:
To be clear, the form on the Show view for the controller Employee contains a form which is posting to a different controller. We can call it Group.

The form action URL looks like this

/Groups/Create/0/0

The form is declared as follows

Html.BeginForm("Create", "Groups")

After trying many different overloads for Html.BeginForm I have found that the parameters are only mapped when the form action URL matches the current URL in the browser address bar.

So if i navigate to the URL /Groups/Create/1/42 I will have a new form. If I then submit the form, the URL route values are passed to the POST action.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浅忆流年 2024-09-01 04:07:04

如果我正确理解您的问题,您希望呈现的

元素的 action 指向包含路由值的 URL。这应该可以通过 HtmlHelper.BeginForm() 扩展方法的重载之一实现:

Html.BeginForm("action","controller", new { idOne=1, idTwo=2 }, FormMethod.Post);

如果我的问题全部错误,请告诉我:)

If I understand your question correctly, you want the action of the rendered <form> element pointing to URL containing route values. This should be possible with one of the overloads of the HtmlHelper.BeginForm() extension method:

Html.BeginForm("action","controller", new { idOne=1, idTwo=2 }, FormMethod.Post);

Let me know if I got your question all wrong :)

暖阳 2024-09-01 04:07:04

我很确定您只能从表单内的输入中发布表单数据。您是否考虑过以从 URL 创建表单输入值的方式呈现视图(也许使用 HTML 帮助程序?)。

更新:如果您根本不想使用表单,请使用ControllerContext.RouteData.Values["idOne"],而不是通过方法签名传递它。

I'm pretty sure that you can only post form data from inputs inside the form. Have you considered rendering the view in such a way to create form input values off of the URL (perhaps with an HTML helper?).

UPDATE: If you don't want to use the form at all, use ControllerContext.RouteData.Values["idOne"] as opposed to passing it in through the method signature.

苍景流年 2024-09-01 04:07:04

我也遇到了类似的问题,配置RouteConfig已经解决了这个问题。

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
             ...
             routes.MapRoute(
             "MyRoute",
             "{controller}/{action}/{idOne}/{idTwo}",
             new
             {
                 controller = "Employee", // as an example
                 action = "Show",
                 idOne = UrlParameter.Optional,
                 idTwo= UrlParameter.Optional
             }, new { idOne = @"\d{1,5}" });

        }
    }

...和...
Html .BeginRouteForm()

@using (Html.BeginRouteForm("MyRoute", new { idOne = 1, idTwo= 2 }, FormMethod.Post))

将开始标记写入响应。当用户提交时
表单中,请求将由路由目标处理。
该成员超载。有关该会员的完整信息,
包括语法、用法和示例,单击重载中的名称
列表。

以及所有作品

I had a similar problem, the configuration RouteConfig has solved this problem.

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
             ...
             routes.MapRoute(
             "MyRoute",
             "{controller}/{action}/{idOne}/{idTwo}",
             new
             {
                 controller = "Employee", // as an example
                 action = "Show",
                 idOne = UrlParameter.Optional,
                 idTwo= UrlParameter.Optional
             }, new { idOne = @"\d{1,5}" });

        }
    }

...And...
Html.BeginRouteForm()

@using (Html.BeginRouteForm("MyRoute", new { idOne = 1, idTwo= 2 }, FormMethod.Post))

Writes an opening tag to the response. When the user submits
the form, the request will be processed by the route target.
This member is overloaded. For complete information about this member,
including syntax, usage, and examples, click a name in the overload
list.

And all works

瞄了个咪的 2024-09-01 04:07:04

我最近也遇到了这个问题,因为我有不同的路线,所以它映射到默认路线,而不考虑我传入的额外路线参数。

因此,为了让它快速工作,我使用 form 编写了表单标签,并使用 @Url.Action 来创建所需的操作。

I recently had this issue too, and because I had a different route, it was mapping to the default route, without taking into account the extra route params that I was passing in.

So to get it working quickly, I wrote the form using form tags instead, and used @Url.Action to create the required action.

烟凡古楼 2024-09-01 04:07:04

进入“创建”视图后,用于到达那里的路线值需要重新发布到后期操作。

因此,一个选择是使用一对隐藏层来保存来自路线的 id。这样,一旦您发布格式,其值就会与其他输入一起发布。

Once you get to your Create view, your route values used to get there need to be re-posted to the post action.

So, one option is having a pair of hiddens, to hold the ids that came from the route. This way, once you post the formas, its values are posted along with the other inputs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文