使用数据库中的选定值添加下拉列表

时间:2016-06-13 16:39:13

标签: asp.net-mvc-3 asp.net-mvc-4

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

我的模特

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>

我正在显示获取下拉列表并显示的代码。 在这里,我获得了下拉列表。但我想获得带有选定文本的列表(存储值应该选择)

怎么做

1 个答案:

答案 0 :(得分:0)

当您选择SelectListItem时,只需添加:

Selected = x.dropdownName == dropdownname

但是,只要ModelState选项列表中的值可以绑定,就不需要这样做。我不确定你在视图中到底做了什么,但是Html.DropDownList的第一个参数应该对应于模型上的属性,因为它需要在post上绑定回该属性。如果它确实匹配属性,则Razor将使用该属性的值自动在选择列表中设置适当的选定值。对于它的价值,使用*For系列助手时会更容易:

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

通过这种方式,你可以通过表达式知道你绑定了一些真实的东西,而不是仅仅希望你的字符串名称是正确的并且会绑定。

相关问题