asp.net-mvc 使用带有查询字符串的 Html.BeginForm
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7323465/
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
Using Html.BeginForm with querystring
提问by Tae-Sung Shin
My url looks like this:
我的网址如下所示:
customer/login?ReturnUrl=home
In the login view, I have used this pattern of code which works fine.
在登录视图中,我使用了这种工作正常的代码模式。
@using(Html.BeginForm())
{
...
}
This magically generates following html
这神奇地生成了以下 html
<form action="customer/login?ReturnUrl=home" method="post">
But now, I need to add an attribute (e.g., data-id="something") in the form. How can I do that? If I don't have any query string, I know I can do something like this:
但是现在,我需要data-id="something"在表单中添加一个属性(例如,)。我怎样才能做到这一点?如果我没有任何查询字符串,我知道我可以这样做:
@using(Html.BeginForm(action, controller, FormMethod.Post,
new { data_id="something" }))
But don't know how to add querystring which should be in html:
但不知道如何添加应该在 html 中的查询字符串:
<form action="customer/login?ReturnUrl=home" method="post" data-id="something">
I thought about using <form>directly but don't know how to specify querystring which is variable. And I have no idea how to achieve it with Html.BeginForm. Any tip would be appreciated.
我想<form>直接使用但不知道如何指定可变的查询字符串。而且我不知道如何使用Html.BeginForm. 任何提示将不胜感激。
RESOLUTION:
解析度:
For now, I used <form>with following hint How to get current url value in View. The resulting view looks like
现在,我使用<form>了以下提示How to get current url value in View。结果视图看起来像
<form action="@Request.Url.PathAndQuery" data-id="something" method="POST">
But it would be nice to have an overloaded method of BeginFormfor this.
但是如果有一个重载的方法会很好BeginForm。
采纳答案by arviman
I guess this doesn't directly answer the question, but why not just use a plain old form tag?
我想这并没有直接回答这个问题,但为什么不直接使用一个普通的旧表单标签呢?
<form action='customer/[email protected]["ReturnUrl"]' method="post" data-id="something">
Alternatively, you can create a custom HtmlHelperExtension that renders a form with path and querystring. In this HtmlHelperExtension you can iterate through your querystring values and populate the routeValueDictionary which you then pass to a Html.BeginForm constructor.
或者,您可以创建一个自定义 HtmlHelperExtension 来呈现带有路径和查询字符串的表单。在此 HtmlHelperExtension 中,您可以遍历查询字符串值并填充 routeValueDictionary,然后将其传递给 Html.BeginForm 构造函数。
If you don't want something so extensible you can just use the overloaded constructor of Html.BeginForm using
@Html.BeginForm("login", "customer", new {ReturnUrl = @Request.QueryString["ReturnUrl"]},FormMethod.Post, new {data-id="something"});
如果你不想要如此可扩展的东西,你可以使用 Html.BeginForm 的重载构造函数
@Html.BeginForm("login", "customer", new {ReturnUrl = @Request.QueryString["ReturnUrl"]},FormMethod.Post, new {data-id="something"});
回答by JustMaier
Here's The way that worked for me
这是对我有用的方法
Html.BeginForm("Profile", "Partner", routeValues: new {id=Partner.partner_id},method:FormMethod.Post)
It was almost like there was a problem with overloading the method, but by specifying what things are, it seems to work fine...
这几乎就像重载方法有问题,但是通过指定内容,它似乎工作正常......
回答by conical
To create a RouteValueDictionary from the querystring:
要从查询字符串创建 RouteValueDictionary:
RouteValueDictionary queryStringDictionary = new RouteValueDictionary(Request.QueryString.AllKeys.ToDictionary(key => key, key => (object)Request.QueryString[key]));
Then you can use it with Html.BeginForm:
然后你可以将它与 Html.BeginForm 一起使用:
Html.BeginForm(null, null, queryStringDictionary, FormMethod.Post, new Dictionary<string, object> { { "autocomplete", "off" } })
回答by savagepanda
using Reflector to look at the code,
使用 Reflector 查看代码,
BeginForm() will pass directly the rawUrl over to the final Form. Any other overloads on BeginForm will go through a helper utility which will strip the query string.
BeginForm() 将直接将 rawUrl 传递给最终的 Form。BeginForm 上的任何其他重载都将通过一个帮助程序实用程序,该实用程序将删除查询字符串。
回答by Sanjeev
This works for me :
这对我有用:
@using (Html.BeginForm("index", "Photos", routeValues: new { user = pUser, album = pAlbum, }, method: FormMethod.Get))
Explicit route values and method is what is required...
需要显式路由值和方法...
回答by Saad A
Just incase you wanted to add other attributes as well. use below code
以防万一您还想添加其他属性。使用下面的代码
@using (Html.BeginForm("actionName", "controllerName", routeValues: new { lang = "en" }, method:FormMethod.Post, htmlAttributes: new { @class= "my-form", enctype = "multipart/form-data" }))
回答by Michael Sagalovich
Try @using(Html.BeginForm(null, null, FormMethod.Post, new { data_id="something" }))
尝试 @using(Html.BeginForm(null, null, FormMethod.Post, new { data_id="something" }))
It should use the default logic to construct the url, just as if you used BeginForm()
它应该使用默认逻辑来构造 url,就像你使用 BeginForm()
(never tried that though in such case, but I believe it should work)
(虽然在这种情况下从未尝试过,但我相信它应该有效)

