我在ma应用程序中有一个数据库驱动的下拉列表,通过它我从中选择一个值并显示与所选选项相关的数据。我这样做他页面重新加载并显示相关数据,但下拉列表的值更改为初始值,并且不保留选定的值。我在下面给了ma代码,请帮助我。
我的控制器操作:FilterCandidatesStatus()是我进行下拉工作的地方,索引操作是针对页面数据有ropdown和te显示值显示的页面
[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 :(得分:0)
您可以将它存储在TempData中,而不是存储在ViewBag中,如下所示:
TempData["Candidates"] = candidates;
并在View中读取之后这样做,所以它在TempData中重新输入,否则它会在第一次读取后从TempData中删除,在视图中这样做:
@{
IEnumerable<Recruitment_System.Entities.CandidateStatus> candidates = (IEnumerable<Recruitment_System.Entities.CandidateStatus>)TempData["Candidates"]
TempData.Keep("Candidates")
}
在你的行动中,你也必须坚持使用Keep in TempData
答案 1 :(得分:0)
以防万一你有兴趣通过强类型视图
试试这个// 您可以在场景中使用的MY变通方法示例
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
}
稍后发帖如果你想在HTTPPOST中加载DDL,只需用上面的foreach循环传递模型..
问候
答案 2 :(得分: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>