How to keep dropdownlist selected value after postback

后端 未结 6 1181
日久生厌
日久生厌 2020-12-19 13:03

In asp.net mvc3 how to keep dropdown list selected item after postback.

6条回答
  •  暖寄归人
    2020-12-19 13:15

    Even easier, you can include the name(s) of your dropdowns in your ActionResult input parameters. Your dropdowns should be in form tags. When the ActionResult is posted to, ASP.Net will iterate through querystrings, form values and cookies. As long as you include your dropdown names, the selected values will be preserved.

    Here I have a form with 3 dropdowns that posts to an ActionResult. The dropdown names are (non-case sensitive): ReportName, Year, and Month.

        //MAKE SURE TO ACCEPT THE VALUES FOR REPORTNAME, YEAR, AND MONTH SO THAT THEY PERSIST IN THE DROPDOWNS EVEN AFTER POST!!!!
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ReportSelection(string reportName, string year, string month)
        {
            PopulateFilterDrowdowns();
            return View("NameOfMyView");
        }
    

提交回复
热议问题