ASP.Net MVC:下拉列表

时间:2016-01-11 19:38:31

标签: c# asp.net-mvc-5

我正在通过webgrid显示数据。 webgrid有文本框和下拉列表,显示每个学生的姓名,州,城市等。我的代码正在运行,但每个学生都显示错误的选定城市。我手动填充我的视图模型,以便于测试。在这里,我正在粘贴我的代码。如果可能请看看,让我知道错误的城市显示的原因。在代码中要改变什么。 我的完整代码在这里

我的dotnet小提琴链接https://dotnetfiddle.net/BrKBx1

请不要告诉我更改模型类设计代码。

ViewModel和Model

public class MainViewModel
{
    public List<Student> Students { get; set; }
    public int SelectedState { get; set; }
    public int SelectedCity { get; set; }
}

public class Student
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int StateID { get; set; }
    public int CityID { get; set; }
    public List<States> States { get; set; }
    public List<Cities> Cities { get; set; }
}

public class States
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class Cities
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int StateID { get; set; }
}

控制器

 public ActionResult Index()
        {

            MainViewModel oVm = new MainViewModel()
            {
                Students = new List<Student>() {
                        new Student
                        {
                            ID=1,
                            Name="JoyDev",
                            StateID=1,
                            CityID=1,
                            States=new List<States>()
                            {
                                new States
                                {
                                    ID=1,
                                    Name="WestBengal",
                                },
                                new States
                                {
                                    ID=2,
                                    Name="Bihar",
                                },
                                new States
                                {
                                    ID=3,
                                    Name="Orrisa",
                                }

                            },
                            Cities=new List<Cities>()
                            {
                                new Cities
                                {
                                    ID=1,
                                    Name="Alipur"
                                },
                                new Cities
                                {
                                    ID=2,
                                    Name="Asansol"
                                },
                                new Cities
                                {
                                    ID=3,
                                    Name="Andul"
                                }

                            }
                        },

    //***********
                        new Student
                        {
                            ID=2,
                            Name="Mukti",
                            StateID=2,
                            CityID=4,
                            States=new List<States>()
                            {
                                new States
                                {
                                    ID=1,
                                    Name="WestBengal",
                                },
                                new States
                                {
                                    ID=2,
                                    Name="Bihar",
                                },
                                new States
                                {
                                    ID=3,
                                    Name="Orrisa",
                                }

                            },
                            Cities=new List<Cities>()
                            {
                                new Cities
                                {
                                    ID=4,
                                    Name="Janpur"
                                },
                                new Cities
                                {
                                    ID=5,
                                    Name="Madhubani"
                                },
                                new Cities
                                {
                                    ID=6,
                                    Name="Kanti"
                                }

                            }
                        },
    //***********
                        new Student
                        {
                            ID=3,
                            Name="Somnath",
                            StateID=3,
                            CityID=9,
                            States=new List<States>()
                            {
                                new States
                                {
                                    ID=1,
                                    Name="WestBengal",
                                },
                                new States
                                {
                                    ID=2,
                                    Name="Bihar",
                                },
                                new States
                                {
                                    ID=3,
                                    Name="Orrisa",
                                }

                            },
                            Cities=new List<Cities>()
                            {
                                new Cities
                                {
                                    ID=7,
                                    Name="Chandapur"
                                },
                                new Cities
                                {
                                    ID=8,
                                    Name="Dhankauda"
                                },
                                new Cities
                                {
                                    ID=9,
                                    Name="Konarak"
                                }

                            }
                        }


                    }

            };

            return View(oVm);

        }

查看代码

@model  BuildTable.Models.MainViewModel
@{
    ViewBag.Title = "Test Grid";
}

@{
    WebGrid grid = new WebGrid(null);
    grid.Bind(Model.Students);

}
@grid.GetHtml(
        tableStyle: "webgrid-table",
        headerStyle: "webgrid-header",
        footerStyle: "webgrid-footer",
        alternatingRowStyle: "webgrid-alternating-row",
        selectedRowStyle: "webgrid-selected-row",
        rowStyle: "webgrid-row-style",
        mode: WebGridPagerModes.All,
        firstText: "<<",
        previousText: "<",
        nextText: ">",
        lastText: ">>",
        numericLinksCount: 5,
        columns:
            grid.Columns
            (
            grid.Column(columnName: "ID", header: "ID", format: @<text>@item.ID</text>,style:"SmallCols"),
            grid.Column(columnName: "FirstName", header: "First Name", format: @<text><input type="text" id="txtFirstName" value="@item.Name" class="edit-mode" /></text>,style:"NameColWidth" ),

            //grid.Column(columnName: "StateName", header: "State Name", format: @<text>@Html.DropDownListFor(x => x.SelectedState, new SelectList(Model.Students[1].States, "ID", "Name", Model.SelectedState = item.StateID), "-- Select States--", new { id = "cboState", @class = "edit-mode" })</text>,style:"NameColWidth"),

            grid.Column(columnName: "StateName", header: "State Name", format: @<text>@Html.DropDownListFor(x => x.SelectedState, new SelectList(Model.Students.SelectMany(x => x.States).Where(x => x.StateID == item.StateID), "ID", "Name", Model.SelectedState = item.StateID), "-- Select States--", new { id = "cboState", @class = "edit-mode" })</text>,style:"NameColWidth"),
            grid.Column(columnName: "CityName", header: "City Name", format: @<text>@Html.DropDownListFor(x => x.SelectedCity, new SelectList(Model.Students.SelectMany(x => x.Cities).Where(x => x.ID == item.StateID), "ID", "Name", Model.SelectedCity = item.CityID), "-- Select City--", new { id = "cboCity", @class = "edit-mode" })
            </text>,style:"NameColWidth")
            ))

我们不能这样过滤(Model.Students.SelectMany(x => x.Cities).Where(x => x.StateID == item.StateID) 上面的代码返回每个学生的所有城市数据,我在那里根据州id过滤城市。如果这不对 代码,然后建议我写什么来实现相同。感谢

2 个答案:

答案 0 :(得分:1)

您从未设置任何城市的StateID,因此如果您按StateID == item.StateID过滤,则永远不会匹配。

在你的控制器中,你需要这样的东西:

Cities = new List<Cities>()
{
    new Cities
    {
        ID = 4,
        StateID = 1, // add this!
        Name = "Janpur"
    },
    new Cities
    {
        ID = 5,
        StateID = 2, // add this!
        Name = "Madhubani"
    },
    new Cities
    {
        ID = 6,
        StateID = 3, // add this!
        Name = "Kanti"
    }
}

另外,我建议您将SelectLists放入视图模型,而不是在视图中创建它们。这将使您的代码更易于阅读,从概念上讲,SelectList实际上是一种支持视图的模型,它不是视图本身。

这是一个更新的小提琴:https://dotnetfiddle.net/UYpO9m

答案 1 :(得分:0)

因为您的Where来电是 SelectMany之后的。此时,您拥有的只是Cities的集合,因此当您按x.ID == item.StateID过滤时,您只说CitiesCities.ID等于item.StateID的实例}。没有,根据您的测试数据,所以没有返回任何内容。更有可能的是,您只需要在Where来电之前将SelectMany电话移至,然后将表达式更改为x.StateID == item.StateID