添加具有从数据库中选择的值的下拉列表

adding dropdown list with selected value from database

如何从数据库中获取 selected drown drop 值?我将员工详细信息存储在一个 table 中,其中包含下拉 selected 值(整数)和其他 table 下拉值和文本。

我的模特是

public class EmployeeDetails
{
    public int empid {set;get;}
    public string empname{set;get;}
    public string empdesig{set;get;}
    public decimal empsal{set;get;}
    public int education{set;get;}
    public DateTime time{set;get;}
    public string dropdownname { set; get; }
}

public class DropDownList
{
    public int dropdownId { set; get; }
    public string dropdownName { set; get; }
}

public class DisplayEmployeeViewModel
{
    public IEnumerable<EmployeeDetails> EmployeeDetails { set; get; }
    public DisplayCreateEmployeeViewModel createemployee { set; get; }
    public string TabName { set; get; }
    public List<SelectListItem> DropdownSelectListItems
    {
        get
        {
            var dropdown = (new DropdownRepository()).GetDropdownTypes();
            var entityList = dropdown.Select(x => new SelectListItem()
            {
                Text = x.dropdownName,
                Value = x.dropdownId.ToString()
            });
            return entityList.ToList();
        }
    }
}

public class DisplayCreateEmployeeViewModel
{
    public EmployeeDetails EmployeeDetails { set; get; }
    public int empid { set; get; }
    public string empname { set; get; }
    public string empdesig { set; get; }
    public decimal empsal { set; get; }
    public int education { set; get; }
    public DateTime time { set; get; }
    public string dropdownname { set; get; }
    public List<SelectListItem> DropdownSelectListItems
    {
        get
        {
            var dropdown = (new DropdownRepository()).GetDropdownTypes();
            var entityList = dropdown.Select(x => new SelectListItem()
            {
                Text = x.dropdownName,
                Value = x.dropdownId.ToString()
            });
            return entityList.ToList();
        }
    }
}

查看

<div>
    <table id="tblemployeedetails" class="table table-hover table-bordered table-responsive "  >
        <thead style="background-color:black;color:white; ">
            <tr>
                <th></th>
                <th>Employee Name</th>
                <th>Employee Designation</th>
                <th>Enployee Education</th>
                <th>Employee Salary</th>
            </tr>
        </thead>
        <tbody>
            @foreach (EmployeeDetails details in Model.EmployeeDetails)
            {
                <tr>
                    <td>@Html.CheckBox("status" + details.empid, new {value=details.empid,@class="detailCheckBox" })</td>
                    <td>@Html.TextBox(details.empname, details.empname, new {@class="txtdisable" })</td>
                    <td>@Html.TextBox(details.empdesig, details.empdesig, new { @class = "txtdisable" })</td>
                    <td>@Html.DropDownList(details.education.ToString(),Model.DropdownSelectListItems, new { @class = "txtdisable" })</td>
                    <td>@Html.TextBox(details.empsal.ToString(), details.empsal, new { @class = "txtdisable" })</td>
                </tr>
            }
        </tbody>        
    </table>
</div>

我显示的代码获取下拉完整列表并显示。 在这里,我得到了下拉列表。但我想获得带有 selected 文本的列表(存储值应该 select)

怎么做

当您select进入SelectListItem时,只需添加:

Selected = x.dropdownName == dropdownname

然而,这真的不应该是必需的,只要 ModelState 中有一个值,select 列表就可以绑定到。我不确定你在视图中到底在做什么,但是 Html.DropDownList 的第一个参数应该对应于你模型上的 属性,因为它需要这样才能绑定回那个属性 在 post 上。如果它确实匹配 属性,那么 Razor 将使用 属性 的值自动在 select 列表中设置适当的 selected 值。值得一提的是,使用 *For 帮手家族时,这要容易得多:

@Html.DropDownListFor(m => m.SomeProperty, Model.SomePropertyChoices)

这样,您就可以通过表达式知道您绑定的是真实的东西,而不是仅仅希望您的字符串名称是正确的并且会绑定。