我是mvc的新手,我通过以下方式填充下拉列表:
public ActionResult People(bool Connected, int? page, FilterNurseSearch objFilterNurseSearch)
{
if (ApplicationSession.CurrentUser == null)
{
//redirect
return RedirectToAction("Login", "Account");
}
long NurseId = ApplicationSession.CurrentUser.NurseId;
NurseProfile objNurseProfile = objNurseDAL.GetNurseProfileByNurseId(NurseId);
DropDown();
((List<SelectListItem>)ViewBag.Profession).FirstOrDefault(x => x.Value == objNurseProfile.ProfessionId.ToString()).Selected = true;
if (objFilterNurseSearch.Profession != null && objFilterNurseSearch.Profession != Convert.ToString(0))
{
objFilterNurseSearch.Profession = objFilterNurseSearch.Profession;
}
else
{
if (objFilterNurseSearch.Profession == Convert.ToString(0) )
{
objFilterNurseSearch.Profession = null;
}
else if (objNurseProfile.Profession != null)
{
objFilterNurseSearch.Profession = objNurseProfile.ProfessionId.ToString();
}
}
//objFilterNurseSearch.Profession = objNurseProfile.ProfessionId.ToString();
ViewBag.FilterNurseSearch = objFilterNurseSearch;
Result objResult = objNurseDAL.GetNurses(NurseId, Connected, objFilterNurseSearch);
int pageSize = 12;
int pageNumber = (page ?? 1);
ViewBag.Connected = Connected;
return View(((List<NurseProfile>)objResult.Results).ToPagedList(pageNumber, pageSize));
}
这是我在HTML中使用它的方式:
@Html.DropDownList("Profession", (List<SelectListItem>)ViewBag.Profession, new { @class = "form-control" })
我想知道如何设置ViewBag中作为预选值而来的值。
我需要在DropDown中显示所选的值。