asp.net-mvc 如何使用 Html.BeginForm() 传入 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/878330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:34:27  来源:igfitidea点击:

How to pass in ID with Html.BeginForm()?

asp.net-mvc

提问by mrblah

In ASP.NET MVCI'm using the HTML helper

ASP.NET MVC我使用 HTML helper

Html.BeginForm("ActionName", "Controller", FormMethod.Post);

But I need to post to: /controller/action/23434

但我需要发布到:/controller/action/23434

How do I pass in the ID?

身怎么交?

回答by Jonathan Freeland

Matt's should work fine. If you are still passing in FormMethod.Post, though, you need to do it like this:

马特的应该工作正常。FormMethod.Post但是,如果您仍然传入,则需要这样做:

Html.BeginForm("action","controller", new { Id = 12345 }, FormMethod.Post);

Reversing the third and fourth parameters will result in the Idbeing treated as an attribute instead of a route value.

反转第三个和第四个参数将导致将Id其视为属性而不是路由值。

回答by Matt Hinze

Html.BeginForm("action", "controller", new {Id = 12345})

Html.BeginForm("action", "controller", new {Id = 12345})

回答by Rich

Html.BeginForm("action", "controller", new { id = ViewBag.FileID },
FormMethod.Post, new { id = "feedbackform" })

As for the querystring, ?type=golden, I don't know how to do that. Of course, a querysting is a get, and undermines the whole purpose of FormMethod.Post. I mean, you could use FormMethod.Get, if you want querystring data, and this might be what you are looking for.

至于查询字符串,?type=golden我不知道该怎么做。当然,查询语句是获取,并且破坏了FormMethod.Post. 我的意思是,FormMethod.Get如果你想要查询字符串数据,你可以使用, 这可能就是你正在寻找的。

Additionally, you can avoid html.beginformand do the querystring, get + post, manually with a form tag.

此外,您可以html.beginform使用表单标签手动避免和执行查询字符串、获取 + 发布。

Thirdly, if you are using the form, you can make a hidden field:

第三,如果你使用表单,你可以做一个隐藏字段:

[input type=hidden name="type" value="golden"]

Then, when the submit button is pressed the value is passed properly as a form variable.

然后,当按下提交按钮时,该值作为表单变量正确传递。