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": // もう少し作業を中断します。} } return RedirectToAction("SomeAction"); }

特定の状況下で、空のクエリ文字列値が一部のブラウザー (IE だと思います) で問題を引き起こすのを見てきました。「Select...」や値の -1 などの値なしではなく、DropDownList にトークンを入力することをお勧めします。

var dropDownList = new List<SelectListItem>();

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

その後

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

またはそのようなもの。

これが役立つことを願っています:)

于 2012-11-01T21:48:30.017 に答える