0

my view have two dropdown and one submit buttom if no value is selected and if form get sumbited with GET method then my URL will be http://localhost:53372/question/index?Index=List&type=&stage=&mid=1&mod=5.

but i m applying an ActionFilter with OnActionExcuting() overriden method. so after submitting form URL is like http://localhost:53372/question/index?index=List&mid=1&mod=5.

where other two QueryString is gone?

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

but if i select Dropdown value then all queryString is in URL.

QueryString with no values stage=&type= are not allowed?

4

1 回答 1

0

默认情况下,MVC 将数据作为查询字符串传递,除非您提交表单,在这种情况下,查询字符串被捆绑为 HttpRequest 对象的一部分。您可以直接使用FormCollection对象或作为对象的一部分访问查询字符串HttpRequest。访问查询字符串最直接的方法是通过FormCollection对象如下 [HttpPost] public ActionResult SubmitData(FormCollection form) { foreach(var key in form.AllKeys) { switch(key) { case "stage": // do some工作休息;case "type": // 做更多工作 break; } } return RedirectToAction("SomeAction"); }

我已经看到空查询字符串值在某些情况下会导致某些浏览器(我认为是 IE)出现问题。我建议使用标记填充您的 DropDownList,而不是没有值,例如“Select...”和值 -1。

var dropDownList = new List<SelectListItem>();

dropDownList.Add(new SelectListItem{ Text = "Select", Value = "-1"} );

接着

@Html.DropDownList("stage", Model.Stages)

或类似的东西。

我希望这有帮助 :)

于 2012-11-01T21:48:30.017 回答