我有两个下拉列表。第二个列表是基于第一个列表填充的,我正在使用JSON来加载它。我现在面临的问题是 发布后,我无法在下拉列表中维护之前选择的值。 要明确:验证后,如果失败,我会列出同一页面 但是它将下拉菜单设置为默认选择而不是我选择的选项。我该如何解决?
NB。我使用Entityframework生成的类作为模式。
这是我在视图中的代码:
<div style="float: left">
@Html.Label("lbRegion", "Region*")
@*@Html.ValidationMessage("REGION_CODE", "*")*@
@Html.DropDownList("REGION_CODE", (SelectList)ViewBag.Categories, "Select region code")
@Html.ValidationMessageFor(m => m.REGION_CODE)
</div>
<div class="tested">
@Html.Label("lblSubregion", "Sub Region*")
@*@Html.ValidationMessage("GEO_ZONE_CODE", "*")*@
@*@Html.ValidationMessageFor(m=>m.GEO_ZONE_CODE)*@
<select id="GEO_ZONE_CODE" name="GEO_ZONE_CODE"></select>
</div>
我控制器的代码:
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 => 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 => r.REGION_CODE == regioncode);
var geozonecodes = dd.Select(tt => 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 => 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)
{
//return PartialView("_Psite");
return RedirectToAction("Psite");
}
}
//count = 0;
return PartialView(db1.MN_PSITE.ToList());
}