Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
On autopostback, dropdown list selected index is going back to '0'. How to maintain to not go to zero in MVC3.
My code is
View is:

HTML
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <div>
        <div style="float: left">
            States Filter :</div>
        <div style="float: left; padding-removed 20px">@Html.DropDownList("Areas", ViewData["Areas"] as SelectList, "-select state-", new {@style = "width:200px;" })</div>
    </div>
    <div style=" padding-removed30px">
        
            <input type="image" value="submit" src="../../Images/FilterBrowse.gif" alt="submit Button"  />
        
    </div>
    
}
for each loop table(Gridview)


Controller is:
C#
//[Authorize]
        public ActionResult States()//int? id)
        {
            ViewData["Areas"] = new SelectList(am.FindUpcomingStates222().ToList(), "state_Name", "state_Name");
            var state = am.FindUpcomingStates().ToList();
            if (state != null)
            {
                ViewData["name"] = true;
                return View("States", state);
            }
            else
                return View("NotFound");
        }


[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult States(string _stateName, FormCollection formvalues)
        {
            _stateName = Request.Form["Areas"].ToString();
            ViewData["Areas"] = new SelectList(am.FindUpcomingStates222().ToList(), "state_Name", "state_Name", _stateName);
            if (_stateName == "")
            {                
                return View();
            }
            else
            {                
                var state = am.FindUpcomingStates22(_stateName).ToList();
                if (state != null)
                    return View("States", state);
                else
                    return View("NotFound");
            }
        }


after clicking submit button ddl going to selected index zero
Thanks,
Sampath bejugama
Posted
Updated 29-Feb-12 2:02am
v4
Comments
thatraja 29-Feb-12 7:39am    
Show your code
ZurdoDev 29-Feb-12 8:05am    
Most often this happens when you databind your ddl in Page_Load without checking for IsPostBack
Dylan Morley 29-Feb-12 8:15am    
This is MVC, so won't apply
Mahmud Hasan 29-Feb-12 10:15am    
What did you mean by auto postback? MVC do not have any postback event. Please clarify.
sampath1750 1-Mar-12 1:02am    
Postback is nothing but After clicking image button dropdown list going to selected index zero.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900