Asp.net mvc 如何使用Html.BeginForm()将查询字符串值放入RouteValueDictionary中?
我发现Html.BeginForm会自动使用RawUrl ie.querystringParameters填充routeValueDictionary。但是我需要指定一个HtmlAttribute,所以我需要使用覆盖Asp.net mvc 如何使用Html.BeginForm()将查询字符串值放入RouteValueDictionary中?,asp.net-mvc,html.beginform,querystringparameter,Asp.net Mvc,Html.beginform,Querystringparameter,我发现Html.BeginForm会自动使用RawUrl ie.querystringParameters填充routeValueDictionary。但是我需要指定一个HtmlAttribute,所以我需要使用覆盖 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttribute
public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes)
执行此操作时,QueryString值不会自动添加到RouteValueDictionary。我怎样才能做到这一点
这是我最好的尝试,但似乎不起作用
<% RouteValueDictionary routeValueDictionary = new RouteValueDictionary(ViewContext.RouteData.Values);
foreach (string key in Request.QueryString.Keys )
{
routeValueDictionary[key] = Request.QueryString[key].ToString();
}
using (Html.BeginForm("Login", "Membership", routeValueDictionary, FormMethod.Post, new { @class = "signin-form" }))
{%> ...
但是作为QueryString一部分的returnUrl的值总是空的,除非我在视图中使用默认的无参数Html.BeginForm
谢谢,
贾斯汀你可以写一个助手:
public static MvcHtmlString QueryAsHiddenFields(this HtmlHelper htmlHelper)
{
var result = new StringBuilder();
var query = htmlHelper.ViewContext.HttpContext.Request.QueryString;
foreach (string key in query.Keys)
{
result.Append(htmlHelper.Hidden(key, query[key]).ToHtmlString());
}
return MvcHtmlString.Create(result.ToString());
}
然后:
<% using (Html.BeginForm("Login", "Membership", null, FormMethod.Post, new { @class = "signin-form" })) { %>
<%= Html.QueryAsHiddenFields() %>
<% } %>
您可以编写一个助手:
public static MvcHtmlString QueryAsHiddenFields(this HtmlHelper htmlHelper)
{
var result = new StringBuilder();
var query = htmlHelper.ViewContext.HttpContext.Request.QueryString;
foreach (string key in query.Keys)
{
result.Append(htmlHelper.Hidden(key, query[key]).ToHtmlString());
}
return MvcHtmlString.Create(result.ToString());
}
然后:
<% using (Html.BeginForm("Login", "Membership", null, FormMethod.Post, new { @class = "signin-form" })) { %>
<%= Html.QueryAsHiddenFields() %>
<% } %>
检查Html.BeginForm的源代码并没有太大帮助,但它显示了无参数方法做您想要的事情的原因——它实际上是从请求url设置formAction 如果您希望将querystring保留为querystring,而不是可能成为POST的一部分,那么这里有一个替代扩展:
/// <summary>
/// Turn the current request's querystring into the appropriate param for <code>Html.BeginForm</code> or <code>Html.ActionLink</code>
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
/// <remarks>
/// See discussions:
/// * http://stackoverflow.com/questions/4675616/how-do-i-get-the-querystring-values-into-a-the-routevaluedictionary-using-html-b
/// * http://stackoverflow.com/questions/6165700/add-query-string-as-route-value-dictionary-to-actionlink
/// </remarks>
public static RouteValueDictionary QueryStringAsRouteValueDictionary(this HtmlHelper html)
{
// shorthand
var qs = html.ViewContext.RequestContext.HttpContext.Request.QueryString;
// because LINQ is the (old) new black
return qs.AllKeys.Aggregate(new RouteValueDictionary(html.ViewContext.RouteData.Values),
(rvd, k) => {
// can't separately add multiple values `?foo=1&foo=2` to dictionary, they'll be combined as `foo=1,2`
//qs.GetValues(k).ForEach(v => rvd.Add(k, v));
rvd.Add(k, qs[k]);
return rvd;
});
}
检查Html.BeginForm的源代码并没有太大帮助,但它显示了无参数方法做您想要的事情的原因——它实际上是从请求url设置formAction 如果您希望将querystring保留为querystring,而不是可能成为POST的一部分,那么这里有一个替代扩展:
/// <summary>
/// Turn the current request's querystring into the appropriate param for <code>Html.BeginForm</code> or <code>Html.ActionLink</code>
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
/// <remarks>
/// See discussions:
/// * http://stackoverflow.com/questions/4675616/how-do-i-get-the-querystring-values-into-a-the-routevaluedictionary-using-html-b
/// * http://stackoverflow.com/questions/6165700/add-query-string-as-route-value-dictionary-to-actionlink
/// </remarks>
public static RouteValueDictionary QueryStringAsRouteValueDictionary(this HtmlHelper html)
{
// shorthand
var qs = html.ViewContext.RequestContext.HttpContext.Request.QueryString;
// because LINQ is the (old) new black
return qs.AllKeys.Aggregate(new RouteValueDictionary(html.ViewContext.RouteData.Values),
(rvd, k) => {
// can't separately add multiple values `?foo=1&foo=2` to dictionary, they'll be combined as `foo=1,2`
//qs.GetValues(k).ForEach(v => rvd.Add(k, v));
rvd.Add(k, qs[k]);
return rvd;
});
}
您从未在路由值字典中提到过returnUrl,或者您已经提到了?假定routeValueDictionary变量包含Request.QueryString中的所有键/值对。我可以通过使用new{returnUrl=Request.QueryString[returnUrl]}来破解它,但是如果QueryString中的returnUrl不止一个呢?代码生成的标记是什么样子的?我之所以问这个问题,是因为在这种情况下,您可能会发布到像mysite.com?returnUrl=foobar这样的URL您在路由值字典中从未提到过returnUrl,或者您已经提到了?假定routeValueDictionary变量填充了Request.QueryString中的所有键/值对。我可以通过使用new{returnUrl=Request.QueryString[returnUrl]}来破解它,但是如果QueryString中的returnUrl不止一个呢?代码生成的标记是什么样子的?我问这个问题的原因是,在这种情况下,你可能会发布到像mysite.com这样的URL?returnUrl=foobar你的代码仍然会将重复的键转换为逗号分隔的列表-你的意思是要防止吗?您的注释中没有100%的明确信息。@dtanders我的注释意味着我无法确定如何阻止将它们作为逗号分隔的值添加,因为RouteValueDictionary是一个字典,因此不能有重复的键。您的代码仍然会将重复的键转换为逗号分隔的列表-您的意思是阻止吗?您的评论不是100%清楚。@dtanders我的评论意味着我无法找出如何防止将它们作为逗号分隔的值添加,因为RouteValueDictionary是一个字典,因此不能有重复的键