ASP.NET MVC使用选择标记

时间:2016-07-10 01:03:13

标签: c# asp.net asp.net-mvc

我目前正在使用ViewBag将数据从我的数据库检索到DropDownList,如下所示。我被告知并且在网上看到使用Select Tag时最好使用Strongly Typed而不是ViewBag。有人可以帮我使用ViewModel / Strongly Typed方法让它工作吗?我将不胜感激!!我正在学习MVC,我已经习惯了WebForms并且很难围绕MVC方法:(

员工模型:

    public class Employee
{
    [Key]
    public int EmpId { get; set; }

    [Required]
    public string EmpFirstName { get; set; }

    [Required]
    public string EmpLastName { get; set; }

    public int DeptId { get; set; }
    public Department Department { get; set; }

    public int BldgId { get; set; }
    public Building Building { get; set; }
}

EmployeeController:

public class EmployeeController : Controller
{
    private DataEntryContext _context;

    public EmployeeController(DataEntryContext context)
    {
        _context = context;
    }

    public IActionResult Index()
    {
        return View(_context.Employees.ToList());
    }

    // Populate Department values to DropDownList
    private IEnumerable<SelectListItem> GetDepartments()
    {
        var dept = _context.Departments
            .Select(s => new SelectListItem
            {
                Value = s.DeptId.ToString(),
                Text = s.DeptTitle
            })
            .ToList();

        return (dept);
    }

    // Populate Building values to DropDownList
    private IEnumerable<SelectListItem> GetBuildings()
    {
        var bldg = _context.Buildings
            .Select(b => new SelectListItem
            {
                Value = b.BldgId.ToString(),
                Text = b.BldgName
            })
            .ToList();

        return (bldg);
    }

    public IActionResult Create()
    {
        // Load values to DropDownLists for Departments and Buildings
        ViewBag.DeptListName = GetDepartments();
        ViewBag.BldgListName = GetBuildings();

        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Create(Employee employee)
    {
        if (ModelState.IsValid)
        {
            _context.Employees.Add(employee);
            _context.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(employee);
    }

    public IActionResult Edit(int? id)
    {
        if (id == null)
        {
            return View("Error");
            //return NotFound();
        }

        var employee = _context.Employees
            .Where(e => e.EmpId == id)
            .Single();

        if (employee == null)
        {
            return View("Error");
            //return NotFound();
        }

        ViewBag.DeptListName = GetDepartments();
        ViewBag.BldgListName = GetBuildings();

        return View(employee);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Edit(Employee employee)
    {
        if (ModelState.IsValid)
        {
            ViewBag.DeptListName = GetDepartments();
            ViewBag.BldgListName = GetBuildings();

            _context.Employees.Update(employee);
            _context.SaveChanges();

           return RedirectToAction("Index");
        }

        return View(employee);
    }
}

员工创建视图

以下是我如何检索部门和建筑物下面标签的值。

<select asp-for="DeptId" asp-items="@ViewBag.DeptListName" class="form-control">
                <option>Select Department</option>
            </select>

<select asp-for="BldgId" asp-items="@ViewBag.BldgListName" class="form-control">
                <option>Select Building</option>
            </select>

的ViewModels:

{
public class DeptViewModel
{
    public int DeptId;
    public IEnumerable<SelectListItem> DeptList;
}

}

EmployeeController:

        public IActionResult Create()
    {

        var model = new DeptViewModel { DeptList = DeptList() }; return View(model);

    }

0 个答案:

没有答案