簡體   English   中英

將 Select List 與 PartialView 中的 ViewBag 綁定 - ASP.NET Core MVC

[英]Bind Select List with ViewBag in PartialView - ASP.NET Core MVC

我有兩個模型員工和部門,我正在嘗試使用 ViewBag 在員工創建 PartialView 中將 Select 列表與部門 model 綁定:

    public class Employee
    {
        [Key]
        public int EmployeeId { get; set; }
        
        public string EmployeeName { get; set; }
    
        public string Email{ get; set; }
    
        [ForeignKey("DepartmentId")]
        public int DepartmentId { get; set; }
    
        public virtual Department Department { get; set; }
    }
    
    public class Department
    {
        [Key]
        public int DepartmentId { get; set; }
    
        [Required]
        public string DepartmentName { get; set; }
        public virtual ICollection<Employee> Employees { get; set; }
    }

員工控制器:

public IActionResult Create()
{
    List<Department> departments = _dbcontext.Department.ToList();
    ViewBag.ListDepartments = new SelectList(departments, "DepartmentId", "DepartmentName");
    Employee employee = new Employee();
    return PartialView("_AddEmployeePartialView",employee);
}

_AddEmployeePartialView.cshtml:

    @model WebApplication1.Models.Employee

<div class="modal fade" role="dialog" tabindex="-1" id="addEmployee" aria-labelledby="addEmployeeLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addEmployeeLabel">Employees</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form asp-action="Create" method="post">
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                    
                     ...

                    <div class="form-group">
                        <label asp-for="DepartmentId" class="control-label">DepartmentId</label>
                        <select asp-for="DepartmentId" class="form-control" asp-items="ViewBag.ListDepartments" ></select>

                    </div>
                    ...

                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary">Save</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

但我有一個空的下拉列表。 如何解決這個問題? 是模式彈出窗口中的問題嗎?

我發現了我的錯誤。 在 Controler 中,ViewBage 應該在 Index Action 中而不是在 Create 操作中:

public IActionResult Index()
{
    List<Department> departments = _dbcontext.Department.ToList();
    ViewBag.ListDepartments = new SelectList(departments, "DepartmentId", "DepartmentName");
    Employee employee = _dbContext.Employee.ToList();
    return View(employee);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM