A
A
Anatoly Sergeevich2017-05-15 13:33:40
ASP.NET
Anatoly Sergeevich, 2017-05-15 13:33:40

DropDownList MVC get from base?

Good afternoon, how can I upload data from the database to the DropDownList, I will post all the examples below.
Base "EmployeesDepartments" - Model

public class EmployeesDepartments
    {
        public IEnumerable<SelectListItem> Division { get; set; }

        [AutoIncrement]
        public int Id { get; set; }

        public string Lastname { get; set; }

        public string Firstname { get; set; }
}

Base "Division" - Model
public class Division
    {
        [AutoIncrement]
        public int Id { get; set; }

        [Display(Name = "Название отдела")]
        public string Name { get; set; }
    }

//Controller.cs
public ActionResult Index()
        {
            List<EmployeesDepartments> employees = app.db().Select<EmployeesDepartments>().ToList();
            ViewData["EmployeesDepartments"] = employees;

            return View(employees);
        }

index.cshtml
@model БизнесАссистент.App.Models.EmployeesDepartments
@{
    ViewBag.Title = "Добавить нового работника";
}
    <div class="form-group">
        
        <div class="col-md-7">
            @Html.DropDownListFor(
                x => x.Division,
                new SelectList(Model.Division, "Value", "Text")
            );
        </div>
    </div>

Where an error is made, nothing is displayed simply.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kovalsky, 2017-05-15
@dmitryKovalskiy

The model should not contain the ears of the drop-down list. SelectListItem in the model is already a crutch to write less code, while dragging the dependency of the drop-down list interface all the way to the base itself. What's stopping you from writing a LINQ query that explicitly reshapes the division model into a SelectListItem list - explicitly specifying which parameters will be Key and which Value. The base should not know about any SelectListItem. It's all View layer code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question