如何存储和显示SelectList数据

时间:2019-06-30 22:30:47

标签: asp.net-mvc entity-framework-6

我正在使用SelectList在视图中填充下拉列表。它适用于“创建”和“编辑”视图,以将ID值数据存储在表中。如何检索“名称”值以显示在“详细信息”视图中?

模型

Public Class Employee {
 [Key]
 public int ID { get; set;}
 public string UserName {get; set; }
 public byte Gender { get; set; }
}

ViewModel

public class EmployeeEditViewModel {
 public int ID { get; set; }
 public string UserName { get; set; }
 public SelectList GenderList { get; set; }

 public EmployeeEditViewModel () {
        GenderList = CommonHelper.GenderList(null);
    }
}

助手

 public static SelectList GenderList(object selected)
    {
        return new SelectList(new[]
        {
            new { Value = 0, Name = "Male" },
            new { Value = 1, Name = "Female" }
            }
            , "Value", "Name", selected);
    }

修改视图

@model Models.ViewModel.EmployeeEditViewModel
@using (Html.BeginForm()) {
@Html.HiddenFor(model => model.ID)
<div class="form-group">
    @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(model => model.GenderList, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.Gender, Model.GenderList, "- Select -", new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.GenderList, "", new { @class = "text-danger" })
    </div>
</div>
}

控制器

[HttpPost]
    public ActionResult CreateEmployee(EmployeeEditViewModel emProfile)
    {
        try
        {
            if (ModelState.IsValid)
            {
                Employee newUser = new Employee();
                newUser.UserName = emProfile.UserName;
                newUser.Gender = emProfile.Gender;

                userRepository.Add(newUser);
                userRepository.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        catch (Exception ex)
        { }            
        return View(emProfile);
    }

到目前为止,它运行良好,我能够创建,编辑Employee记录,并且在性别表中存储了1或0。 但是,当我想在详细信息视图中显示员工数据时,如何获得文本“男性”或“女性”?

1 个答案:

答案 0 :(得分:0)

我最终创建了一个辅助方法来检索文本。

public static string GetTextFromSelectList(int id, string listName)
    {
        string selectedText = string.Empty;
        SelectListItem selectedItem;

        switch (listName)
        {
            case "Gender":
                selectedItem = Helper.CommonHelper.GenderList(null).FirstOrDefault(x => x.Value == id.ToString());
                selectedText = selectedItem == null ? null : selectedItem.Text;
                break;                
            default:
                selectedText = null;
                break;
        }
        return selectedText;
    }