据我所知,只有无参数版本BeginForm
使用当前的完整 URL。
public static MvcForm BeginForm(this HtmlHelper htmlHelper) {
// generates <form action="{current url}" method="post">...</form>
string formAction = htmlHelper.ViewContext.HttpContext.Request.RawUrl;
return FormHelper(htmlHelper, formAction, FormMethod.Post, new RouteValueDictionary());
}
我不确定这是否是最好的方法,但您可以编写一个自定义表单助手来包含这些QueryString
值:
public static class MyFormExtensions
{
public static MvcForm MyBeginForm(this HtmlHelper htmlHelper, object htmlAttributes)
{
var rvd = new RouteValueDictionary(htmlHelper.ViewContext.RouteData.Values);
var queryString = htmlHelper.ViewContext.HttpContext.Request.QueryString;
foreach (string key in queryString.AllKeys) rvd.Add(key, queryString[key]);
return htmlHelper.BeginForm(null, null, rvd, FormMethod.Post, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
}
@using (Html.MyBeginForm(new { id = "myform" }))
{
//...
}