将 Html.BeginForm 与查询字符串一起使用
我的网址如下所示:
customer/login?ReturnUrl=home
在登录视图中,我使用了这种工作正常的代码模式。
@using(Html.BeginForm())
{
...
}
这神奇地生成了以下 html
<form action="customer/login?ReturnUrl=home" method="post">
但现在,我需要在表单中添加一个属性(例如,data-id="something"
)。我怎样才能做到这一点?如果我没有任何查询字符串,我知道我可以这样做:
@using(Html.BeginForm(action, controller, FormMethod.Post,
new { data_id="something" }))
但不知道如何添加应该在html中的查询字符串:
<form action="customer/login?ReturnUrl=home" method="post" data-id="something">
我考虑过直接使用
解决方案:
目前,我使用了
<form action="@Request.Url.PathAndQuery" data-id="something" method="POST">
,但是最好有一个 BeginForm
的重载方法来实现这一点。
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
<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:
@using(Html.BeginForm(action, controller, FormMethod.Post,
new { data_id="something" }))
But don't know how to add querystring which should be in 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.
RESOLUTION:
For now, I used <form>
with following hint How to get current url value in View. The resulting view looks like
<form action="@Request.Url.PathAndQuery" data-id="something" method="POST">
But it would be nice to have an overloaded method of BeginForm
for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是对我有用的方法,
这几乎就像重载方法存在问题,但通过指定事物是什么,它似乎工作得很好......
Here's The way that worked for me
It was almost like there was a problem with overloading the method, but by specifying what things are, it seems to work fine...
我想这并不能直接回答问题,但为什么不直接使用普通的旧表单标签呢?
或者,您可以创建一个自定义 HtmlHelperExtension 来呈现带有路径和查询字符串的表单。在此 HtmlHelperExtension 中,您可以迭代查询字符串值并填充routeValueDictionary,然后将其传递给 Html.BeginForm 构造函数。
如果您不想要如此可扩展的东西,您可以使用 Html.BeginForm 的重载构造函数
@Html.BeginForm("login", "customer", new {ReturnUrl = @Request.QueryString["ReturnUrl"]},FormMethod.Post, new {data-id="something"});
I guess this doesn't directly answer the question, but why not just use a plain old form tag?
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.
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"});
要从查询字符串创建 RouteValueDictionary:
然后您可以将其与 Html.BeginForm 一起使用:
To create a RouteValueDictionary from the querystring:
Then you can use it with Html.BeginForm:
使用Reflector查看代码,
BeginForm()将直接将rawUrl传递给最终的Form。
BeginForm 上的任何其他重载都将通过辅助实用程序来删除查询字符串。
using Reflector to look at the code,
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.
以防万一您还想添加其他属性。使用下面的代码
Just incase you wanted to add other attributes as well. use below code
这对我有用:
需要明确的路由值和方法......
This works for me :
Explicit route values and method is what is required...
尝试
@using(Html.BeginForm(null, null, FormMethod.Post, new { data_id="something" }))
它应该使用默认逻辑来构造 url,就像您使用 < code>BeginForm()
(虽然在这种情况下从未尝试过,但我相信它应该有效)
Try
@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()
(never tried that though in such case, but I believe it should work)