0

I have two models Employee and Department, i am trying to bind a Select List with Department model in Employee Create PartialView using ViewBag:

    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; }
    }

EmployeeController:

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>

But i got an empty Dropdownlist. How to resolve this? is the problem in the modal popup??

1 Answer 1

0

I found my mistake. In the Controler the ViewBage should be in Index Action not in Create action:

public IActionResult Index()
{
    List<Department> departments = _dbcontext.Department.ToList();
    ViewBag.ListDepartments = new SelectList(departments, "DepartmentId", "DepartmentName");
    Employee employee = _dbContext.Employee.ToList();
    return View(employee);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.