DropdownList选择的值未选中

时间:2016-02-16 22:09:22

标签: model-view-controller

我有两个实体:部门和部门类型。 在部门的索引视图中,我显示部门列表,当点击按钮时,模态会打开并显示部门的详细信息。

在此模式中,具有部门类型的部门会显示下拉列表。我无法使用我在控制器中设置的值来选择此下拉列表。

这是代码,是我想要做的示例代码。 型号:

public class Department
{
    public int DepartmentId { get; set; }
    public string Name { get; set; }
    public DepartmentType DepartmentType { get; set; }
    public SelectList DepTypeSelectList { get; set; }
}

public class DepartmentType
{
    public int TypeId { get; set; }
    public string Description { get; set; }
}

public class DepartmentDBContext : DbContext
{
    public DbSet<Department> Departments { get; set; }
    public DbSet<DepartmentType> DepartmentType { get; set; }
}

控制器:

public ActionResult Index()
{
    var list = new List<Department>();
    list.Add(new Department{ DepartmentId = 1, Name = "Department 1", DepartmentType = new DepartmentType{ TypeId = 2, Description = "Type 2"}});
    var deptList = new List<DepartmentType>
    {
        new DepartmentType {TypeId = 1, Description = "Type Desc 1"},
        new DepartmentType {TypeId = 2, Description = "Type Desc 2"}
    };
    var selectList = new SelectList(deptList, "TypeId", "Description", 2);
    list[0].DepTypeSelectList = selectList;
    ViewBag.TypeId = new SelectList(deptList, "TypeId", "Description");
    return View(list);
}

部门的索引视图:

@model IEnumerable<ValidationTest.Models.Department>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @{
                    var divName = "#myModal" + item.DepartmentType.TypeId;
                }
                <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="@divName">Open Modal</button>
                @Html.Partial("_DepartmentType", item)

            </td>
        </tr>
    }

</table>

部分查看部门类型:

@model ValidationTest.Models.Department

@{
    var divName = "myModal" + Model.DepartmentType.TypeId;
}

<!-- Modal -->
<div id="@divName" class="modal fade" role="dialog">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Department Type</h4>
            </div>
            <div class="modal-body">
                <p>Type Id: @Model.DepartmentType.TypeId</p>
                <p>Description: @Model.DepartmentType.Description</p>
                <p>
                    @Html.DropDownList("TypeId", Model.DepTypeSelectList, "-- Select Type --", new { @class = "form-control input-sm" })
                </p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>

    </div>
</div>

任何帮助? 感谢

1 个答案:

答案 0 :(得分:0)

我刚用这段代码解决了我的问题:

型号:



    public class Department
    {
        public int DepartmentId { get; set; }
        public string Name { get; set; }
        public int DepartmentTypeId { get; set; }
        public DepartmentType DepartmentType { get; set; }

        //Select list stuf
        public string SlectedValue { get; set; } //DropDownList selected values
        public List DepTypeSelectList { get; set; } // CountryList 
    }


控制器:



    public ActionResult Index()
    {
        List items = new List();
        items.Add(new SelectListItem() { Text = "Type 1", Value = "1" });
        items.Add(new SelectListItem() { Text = "Type 2", Value = "2" });
        items.Add(new SelectListItem() { Text = "Type 3", Value = "3" });
        items.Add(new SelectListItem() { Text = "Type 4", Value = "4" });
        items.Add(new SelectListItem() { Text = "Type 5", Value = "5" });

        var list = new List
        {
            new Department
            {
                DepartmentId = 1,
                Name = "Department 1",
                DepartmentTypeId = 2,
                DepartmentType = new DepartmentType {TypeId = 2, Description = "Type 2"},
                SlectedValue = "2",
                DepTypeSelectList = items
            },
            new Department
            {
                DepartmentId = 2,
                Name = "Department 2",
                DepartmentTypeId = 1,
                DepartmentType = new DepartmentType {TypeId = 1, Description = "Type 1"},
                SlectedValue = "1",
                DepTypeSelectList = items
            },
            new Department
            {
                DepartmentId = 3,
                Name = "Department 3",
                DepartmentTypeId = 2,
                DepartmentType = new DepartmentType {TypeId = 2, Description = "Type 2"},
                SlectedValue = "2",
                DepTypeSelectList = items
            }
        };

        return View(list);
    }


查看:


        @model IEnumerable
    

@Html.ActionLink("Create New", "Create")

@Html.DisplayNameFor(model => model.Name) @foreach (var item in Model) { @Html.DisplayFor(modelItem => item.Name) @{ var divName = "#myModal" + item.DepartmentType.TypeId; } Open Modal @Html.Partial("_DepartmentType", item) }

部分视图:


    @model ValidationTest.Models.Department
    @{
        var divName = "myModal" + Model.DepartmentType.TypeId;
    }
    
    
        

            
            
                
                    ×
                    Department Type
                
                
                    

Type Id: @Model.DepartmentType.TypeId

Description: @Model.DepartmentType.Description

@Html.DropDownListFor(model => model.SlectedValue, Model.DepTypeSelectList, "Please select", new { id = "DepartmentTypeId" })

Close

现在一切正常。 我之所以创建多个表单是因为在索引中我有一个行列表(部门列表),当单击一个时,必须打开一个弹出窗口,你应该能够编辑所选的部门。

确实有很多形式,但它们之间并没有崩溃,我也不知道其他方法。客户请求是索引中的列表,带有可编辑的弹出窗口(模态)。

干杯