如何设置默认值,该值是通过ViewBag从模型中为ASP.NET MVC DropDownList预选的

时间:2019-01-15 05:30:22

标签: html-select html.dropdownlistfor viewbag

我是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中作为预选值而来的值。

Adding the image which shows the value in the ViewBag

我需要在DropDown中显示所选的值。

0 个答案:

没有答案