0

I have a partialview _Psite which contains two dropdownlist and a text box, second one is based one first as Jsonresult (cascading dropdowns). So now suppose if customer select values in first dropdownlist, second one will load based on jquery and json.Then when he enter wrong values in text box validation fails(Session["username"] == null) it will display the same partial view after post in order to reenter .The problem now i am facing is the two dropdownlist is resetting in to default values.I have googled but couldn't find a solution

Following is view of _Psite

@using (Ajax.BeginForm("_firstGridAll", "mnis", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "PsitegridContent" }))
{
  <div style="float: left">
    @Html.DropDownList("REGION_CODE", (SelectList)ViewBag.Categories, "Select region code")
    @Html.ValidationMessageFor(m => m.REGION_CODE)
  </div>
  <div class="tested">
    <select id="GEO_ZONE_CODE" name="GEO_ZONE_CODE"></select>
  </div>
  <div class="tested">
    @Html.TextBoxFor(m => m.PSITE_ID)
    @Html.ValidationMessageFor(m => m.PSITE_ID)
  </div>
  <div class="Testedagain">
    <input type="submit"  value="Search" />
  </div>
}

Controller is

public ActionResult _Psite()
{
  if (TempData["values"].ToString() == "value persists")
  {
    ViewBag.change = true;
    //    ViewBag.Categories =  TempData["EnterUniqueKeyHere"];
    //    return PartialView("_failValidation");
  }
  var categories = db1.MN_PSITE.Select(c => new
  {
    REGION_CODE = c.REGION_CODE,
    CategoryName = c.REGION_CODE
  }).Distinct().ToList();
  ViewBag.Categories = new SelectList(categories, "REGION_CODE", "CategoryName");
  ViewBag.error = false;
  ViewBag.change = false;
  return PartialView();
}

and the controller for validating data is following

[HttpPost]
public ActionResult _firstGridAll(string REGION_CODE, string GEO_ZONE_CODE, string PSITE_ID)
{
  if (ModelState.IsValid == true)
  {
    Session["username"] = null;
    var items = db1.MN_PSITE.Where(x => x.REGION_CODE == REGION_CODE).Where(y => y.GEO_ZONE_CODE == GEO_ZONE_CODE).Where(z => z.PSITE_ID == PSITE_ID);
    //db1.MN_PSITE.Where(x => x.REGION_CODE == Region).Where(y => y.GEO_ZONE_CODE == GeoZONE).Where(z => z.PSITE_ID == Psiteid);
    foreach (var it in items)
    {
      Session["username"] = it.PSITE_SLNO.ToString();
      return PartialView(items.ToList());
    }
    if (Session["username"] == null)  //validation fails
    {
      TempData["error"] = "value doesnot exisit,please renter the details";           
      return RedirectToAction("_Psite");
    }
  }
  //count = 0;
  return PartialView(db1.MN_PSITE.ToList());
}

UPDATE

i am using Entityframework generated classes as model no view viewmode ,do here 'db' is an instance of entity class

peter
  • 8,158
  • 21
  • 66
  • 119
  • Are you using a ViewModel? If so, can you add your ViewModel code? – ekad May 23 '15 at 07:11
  • 4
    You really need to start using view models! Create a view model including at least 2 properties for binding the selected values to, and 2 `IEnumerable` properties for the select lists. In the GET method, the second will be an empty list, but in the POST method, you have both selected values and can repopulate the select lists before returning the view. –  May 23 '15 at 07:13
  • 1
    @peter - Stephen Muecke has given the answer in the form of a comment. Accept it. :) – Dhrumil May 23 '15 at 07:14
  • You're also using `ViewBag` too much, which isn't a good practice. Learn more here: [MVC ViewBag Best Practice](http://stackoverflow.com/q/11262034/1905949) and here: [Is using ViewBag in MVC bad?](http://stackoverflow.com/q/4766062/1905949) – ekad May 23 '15 at 07:16
  • @ekad i am using Entityframework generated classes as model no view viewmode ,do here 'db' is an instance of entity class – peter May 23 '15 at 08:22
  • Using Entityframework generated classes as the model is also a bad practice. Please follow @StephenMuecke's comment above and learn more about view model here: [What is ViewModel in MVC?](http://stackoverflow.com/q/11064316/1905949) – ekad May 23 '15 at 08:26
  • @ekad If you want to kill a mouse, do you need missile or stick sufficient ?:) my website is not that much big and i have to deliver customer early – peter May 23 '15 at 08:33

1 Answers1

1

If you were posting a view model into your action instead of individual parameters, then you would be able to simply pass that model back out in your partial at the end of the action.

It will only be a small class with a few properties, so will take a minute to create. Do it. It will give you what you want.

ozz
  • 5,098
  • 1
  • 50
  • 73