title: How to maintain last selected value in dropdownlist by suing viewbag in MVC
body: <p>I have two dropdown lists. The second list is populated based on first one, and I'm using JSON to load that. The problem I'm facing now is that
  after posting, I'm unable to maintain the previously selected values in the dropdown.
  To be clear: after validation, if it fails I am listing the same page 
  but it is setting the dropdown to the default selections rather than the ones I chose. How can I fix it?</p>
  
  <p>NB. I am using Entityframework generated classes as mode.</p>
  
  <p>This is my code in the view:</p>
  
  <pre><code>&lt;div style="float: left"&gt;
      @Html.Label("lbRegion", "Region*")
      @*@Html.ValidationMessage("REGION_CODE", "*")*@
  
      @Html.DropDownList("REGION_CODE", (SelectList)ViewBag.Categories, "Select region code")
      @Html.ValidationMessageFor(m =&gt; m.REGION_CODE)
  &lt;/div&gt;
  
  &lt;div class="tested"&gt;
      @Html.Label("lblSubregion", "Sub Region*")
      @*@Html.ValidationMessage("GEO_ZONE_CODE", "*")*@
      @*@Html.ValidationMessageFor(m=&gt;m.GEO_ZONE_CODE)*@
      &lt;select id="GEO_ZONE_CODE" name="GEO_ZONE_CODE"&gt;&lt;/select&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>And the code for my controller:</p>
  
  <pre><code>public ActionResult _Psite()
  {
      MN_SITE mn = new MN_SITE();
  
  
      //ViewBag.Isupdate = false;
      //return PartialView(db1.MN_PSITE.ToList());
  
      var categories = db1.MN_PSITE.Select(c =&gt; new
      {
          REGION_CODE = c.REGION_CODE,
          CategoryName = c.REGION_CODE
      }).Distinct().ToList();
      ViewBag.Categories = new SelectList(categories, "REGION_CODE", "CategoryName");
  
      return PartialView();
  }
  
  public JsonResult Getgeozones(string regioncode)
  {
  
      var dd = db.MN_GEO_ZONE.Where(r =&gt; r.REGION_CODE == regioncode);
      var geozonecodes = dd.Select(tt =&gt; tt.GEO_ZONE_CODE).Distinct().ToList();
      return Json(geozonecodes);
  }
  
  [HttpPost]   
  public ActionResult _firstGridAll(string REGION_CODE, string GEO_ZONE_CODE, string PSITE_ID)
  {
  
      if (ModelState.IsValid == true)
      {
          Session["username"] = null;
          ViewBag.Isupdate = true;
          var items = db1.MN_PSITE.Where(x =&gt; x.REGION_CODE == REGION_CODE).Where(y =&gt; y.GEO_ZONE_CODE == GEO_ZONE_CODE).Where(z =&gt; z.PSITE_ID == PSITE_ID);
  
          //db1.MN_PSITE.Where(x =&gt; x.REGION_CODE == Region).Where(y =&gt; y.GEO_ZONE_CODE == GeoZONE).Where(z =&gt; z.PSITE_ID == Psiteid);
          foreach (var it in items)
          {
              Session["username"] = it.PSITE_SLNO.ToString();
              return PartialView(items.ToList());
          }
          if (Session["username"] == null)
          { 
              //return PartialView("_Psite");
              return RedirectToAction("Psite");
          }
  
      }
      //count = 0;
      return PartialView(db1.MN_PSITE.ToList());
  }
  </code></pre>