将 Html.BeginForm 与查询字符串一起使用

发布于 2024-12-03 08:35:04 字数 1166 浏览 1 评论 0原文

我的网址如下所示:

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">

我考虑过直接使用

但不知道如何指定可变的查询字符串。我不知道如何用 Html.BeginForm 来实现它。任何提示将不胜感激。

解决方案:

目前,我使用了

和以下提示 如何获取View中当前的url值。生成的视图看起来像这样

<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 技术交流群。

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

发布评论

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

评论(7

很快妥协 2024-12-10 08:35:04

这是对我有用的方法,

Html.BeginForm("Profile", "Partner", routeValues: new {id=Partner.partner_id},method:FormMethod.Post)

这几乎就像重载方法存在问题,但通过指定事物是什么,它似乎工作得很好......

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...

情丝乱 2024-12-10 08:35:04

我想这并不能直接回答问题,但为什么不直接使用普通的旧表单标签呢?

 <form action='customer/[email protected]["ReturnUrl"]' method="post" data-id="something">

或者,您可以创建一个自定义 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?

 <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.

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"});

溺深海 2024-12-10 08:35:04

要从查询字符串创建 RouteValueDictionary:

RouteValueDictionary queryStringDictionary = new RouteValueDictionary(Request.QueryString.AllKeys.ToDictionary(key => key, key => (object)Request.QueryString[key]));

然后您可以将其与 Html.BeginForm 一起使用:

Html.BeginForm(null, null, queryStringDictionary, FormMethod.Post, new Dictionary<string, object> { { "autocomplete", "off" } })

To create a RouteValueDictionary from the querystring:

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(null, null, queryStringDictionary, FormMethod.Post, new Dictionary<string, object> { { "autocomplete", "off" } })
背叛残局 2024-12-10 08:35:04

使用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.

浴红衣 2024-12-10 08:35:04

以防万一您还想添加其他属性。使用下面的代码

@using (Html.BeginForm("actionName", "controllerName", routeValues: new { lang = "en" }, method:FormMethod.Post, htmlAttributes: new { @class= "my-form", enctype = "multipart/form-data" }))

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" }))
叶落知秋 2024-12-10 08:35:04

这对我有用:

@using (Html.BeginForm("index", "Photos", routeValues: new { user = pUser, album = pAlbum, }, method: FormMethod.Get))

需要明确的路由值和方法......

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...

匿名的好友 2024-12-10 08:35:04

尝试 @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)

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