2014-04-04 62 views
0

我在ma應用程序中有一個db驅動的下拉列表,通過它從中選擇一個值並顯示與所選選項關聯的數據。溫我這樣做他頁面重新加載和相關的數據顯示,但下拉的值更改爲初始值和選定的值不會保留。我已經在下面給出了ma代碼,請幫助我。如何在MVC回發後保留下拉值

我的控制器操作:FilterCandidatesStatus()是我做的下拉工作和索引作用是頁面DAT具有顯示值ropdown和德同一個頁面

[HttpGet] 
    public ActionResult Index(int? page, int? filter) 
    { 
     ViewBag.statusName = db.CandidateStatuses.ToList(); 
     int pageSize = 12; 
     int pageNumber = (page ?? 1); 
     var candidates = new List<Candidate>(); 

     if (filter != null) 
     { 
      ViewBag.Filter = filter; 
      candidates = db.Candidates.Where(m => m.CandidateStatusID == filter).OrderByDescending(m => m.CandidateID).ToList(); 
     } 
     else 
     { 
      candidates = db.Candidates.OrderByDescending(m => m.CandidateID).ToList(); 
     } 


     return View(candidates.ToPagedList(pageNumber, pageSize)); 
    } 

    [HttpGet] 
    public ActionResult FilterCandidateByStatus(int candidateStatusID) 
    { 

     return RedirectToAction("Index", new { filter = candidateStatusID }); 
    } 

查看:

function fnFilterCandidtates() 
{ 
     window.location = "@Url.Action("FilterCandidateByStatus", "NewCandidate")?  candidateStatusID=" + $("#ddlStatus").val(); 
} 
<select class="h3c1" style=" height:31px; width:115px;" name="list" id="ddlStatus" onchange="fnFilterCandidtates(this)"> 
    <option value="" disabled selected>Select Status</option> 
    @{ 
     foreach (var lstItem in (IEnumerable<Recruitment_System.Entities.CandidateStatus>)@ViewBag.statusName) 
     { 
      <option value="@lstItem.CandidateStatusID">@lstItem.StatusName</option> 
     } 
    } 
</select> 

我只給出了與我的下拉列表相關的代碼。請幫助我。

回答

0

而是在ViewBag存儲,您可以將其存儲在TempData的是這樣的:

TempData["Candidates"] = candidates; 

,並查看閱讀後做這樣所以它的TempData否則它會從TempData的首次讀取之後被刪除remians,做這樣的觀點:

@{ 
IEnumerable<Recruitment_System.Entities.CandidateStatus> candidates = (IEnumerable<Recruitment_System.Entities.CandidateStatus>)TempData["Candidates"] 

TempData.Keep("Candidates") 

} 

在你的動作也一樣,你必須使用TempData的

保持
+0

沒有數據表工作。它的基石一樣。 – Jerry

+0

它應該工作,更新你使用的代碼 –

+0

我很抱歉,我聽到你的編輯代碼。我有一定的疑惑,我不應該在ma指數控制器和視圖中使用viewbag?你可以更新整個代碼wid更改嗎?請。我不清楚它。 – Jerry

0

好堅持以防萬一,如果你的intereste d通過強類型的視圖做

試試看你能在使用這個//我的解決方法例如,你的情況

public ActionResult Index() 
     { 
      LeadSortModel lsm = new LeadSortModel(); // model class 
      var SoucreList = //Pass you list to this 
      List<SelectListItem> lobj = new List<SelectListItem>(); 
      foreach (var item in SoucreList) 
      { 
       lobj.Add(new SelectListItem { Text = item.Lead_Source_Name , Value = item.Lead_Source_Name }); //here i taken text,value same becoz i want on select of text the thing what i selected should be saved not the id we usually :) 
      } 
      lsm.lead_sources = lobj; //leadsources will be present in Models like (public List<SelectedListItem> leadsources ;) . Here you are loading data to model and passing the entire model to make it strongly typed 

      return View(lsm); // return the model to view on initial load @HttpGET 

     } 

後來帖子還櫃面,如果您正在加載DDL在你看來,當HTTPPOST,只是通過該模型與上述foreach循環..

問候

0

我明白了,你將經過選擇的下拉值FilterCandidateByStatus,並從那裏Index。在那裏你給那個值ViewBag.Filter。因此,您可以在視圖中使用該值:

@{ 
    int selectedValue = 0; 
    if(ViewBag.Filter != null) { selectedValue = (int)ViewBag.Filter; } 
} 

function fnFilterCandidtates() 
{ 
     window.location = "@Url.Action("FilterCandidateByStatus", "NewCandidate")?  candidateStatusID=" + $("#ddlStatus").val(); 
} 
<select class="h3c1" style=" height:31px; width:115px;" name="list" id="ddlStatus" onchange="fnFilterCandidtates(this)"> 
<option value="" disabled selected>Select Status</option> 
    @{ 
     foreach (var lstItem in (IEnumerable<Recruitment_System.Entities.CandidateStatus>)@ViewBag.statusName) 
     { 
      if(CandidateStatusID == selectedValue) 
      { 
       <option value="@lstItem.CandidateStatusID" selected>@lstItem.StatusName</option> 
      } 
      else 
      { 
       <option value="@lstItem.CandidateStatusID">@lstItem.StatusName</option> 
      } 
     } 
    } 
</select>