简体   繁体   中英

ASP.Net MVC drop down list not displaying correct data

I've created an employees table and a department table the employee table has a department FK from the departments table.

I'm making an asp.net mvc application database first and when I create the controllers it creates all the views as it should do, but when I go to look at the create page for some reason it's making the second field in the table populate the drop down list.

I'm sorry if this sounds kind of vague or if the question has already been asked on here but I'm struggling to put my problem into words.

Has anyone else ever had this issue? I've tried it with other tables also and its had the same issue unless a table only had 1 field. I know asp.net doesn't display primary keys where the data type is integer, is it perhaps ignoring the primary key then populating it with the next available field?

This is the code in question, it's generated by VS when i create the controller

    <div class="editor-label">
        @Html.LabelFor(model => model.Department, "Department")
    </div>
    <div class="editor-field">
        @Html.DropDownList("Department", String.Empty)
        @Html.ValidationMessageFor(model => model.Department)
    </div>

Found the issue when it creates the select list in the create method in the controller it was populating the list with manager like so.

 public ActionResult Create()
        {
            ViewBag.Department = new SelectList(db.Departments, "Department_Name", "Manager");
            return View();
        }

Instead of this

    public ActionResult Create()
    {
        ViewBag.Department = new SelectList(db.Departments, "Department_Name", "Department_Name");
        return View();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM