Asp.net mvc 3 URL中是否允许查询字符串?
我的视图有两个下拉菜单和一个提交按钮。如果没有选择任何值,并且如果表单使用get方法进行sumbit,那么我的URL将是Asp.net mvc 3 URL中是否允许查询字符串?,asp.net-mvc-3,Asp.net Mvc 3,我的视图有两个下拉菜单和一个提交按钮。如果没有选择任何值,并且如果表单使用get方法进行sumbit,那么我的URL将是http://localhost:53372/question/index?Index=List&type=&stage=&mid=1&mod=5 但是我正在应用一个ActionFilter和OnActionExecuting()重写方法。所以在提交表单后,URL就像http://localhost:53372/question/index?index=List&mid=1&m
http://localhost:53372/question/index?Index=List&type=&stage=&mid=1&mod=5
但是我正在应用一个ActionFilter和OnActionExecuting()
重写方法。所以在提交表单后,URL就像http://localhost:53372/question/index?index=List&mid=1&mod=5
另外两个查询字符串去了哪里?
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.RequestContext.HttpContext.Request.QueryString["mid"] == null || filterContext.RequestContext.HttpContext.Request.QueryString["mod"] == null)
{
mid = Convert.ToString(HttpUtility.ParseQueryString(filterContext.RequestContext.HttpContext.Request.UrlReferrer.Query)["mid"]);
mod = Convert.ToString(HttpUtility.ParseQueryString(filterContext.RequestContext.HttpContext.Request.UrlReferrer.Query)["mod"]);
if (!string.IsNullOrEmpty(mid) || !string.IsNullOrEmpty(mod))
{
RouteValueDictionary redirecttargetDictionary = new RouteValueDictionary();
NameValueCollection Qstr = null;
if (filterContext.HttpContext.Request.RequestType == "GET")
{
Qstr = HttpUtility.ParseQueryString(filterContext.HttpContext.Request.Url.Query);
foreach (string item in Qstr)
{
redirecttargetDictionary.Add(item, Qstr[item]);
}
if (Qstr["mid"] == null)
{
redirecttargetDictionary.Add("mid", mid);
}
if (Qstr["mod"] == null)
{
redirecttargetDictionary.Add("mod", mod);
}
filterContext.Result = new RedirectToRouteResult(redirecttargetDictionary);
}
}
}
}
但若我选择下拉值,那个么所有查询字符串都在URL中
不允许使用没有值的查询字符串
stage=&type=
?默认情况下,MVC将数据作为查询字符串传递,除非您提交表单,在这种情况下,查询字符串被捆绑为HttpRequest对象的一部分。您可以直接使用FormCollection
对象或作为HttpRequest
对象的一部分来访问查询字符串。访问查询字符串的最直接方式是通过FormCollection
对象,如下所示
[HttpPost]
公共行动结果提交数据(表格收集表格)
{
foreach(form.AllKeys中的var键)
{
开关(钥匙)
{
案例“阶段”:
//做些工作
打破
案例“类型”:
//多做些工作
打破
}
}
返回重定向到操作(“SomeAction”);
}
我见过在某些情况下,空查询字符串值会导致某些浏览器(我认为IE)出现问题。我建议在DropDownList中填充一个标记,而不是没有值,例如“选择…”和-1作为值
var dropDownList = new List<SelectListItem>();
dropDownList.Add(new SelectListItem{ Text = "Select", Value = "-1"} );
或者类似的
我希望这能有所帮助:)我不太明白为什么处理查询字符串会这么麻烦。也许你能解释一下?
@Html.DropDownList("stage", Model.Stages)