Asp.net mvc ASP.NET MVC:如何将下拉列表与嵌套列表类型属性绑定

Asp.net mvc ASP.NET MVC:如何将下拉列表与嵌套列表类型属性绑定,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-5,Asp.net Mvc,Asp.net Mvc 4,Asp.net Mvc 5,这是我的全部代码。谁会看到它,然后他们就可以理解我正在努力实现的目标。在看过我的代码后,如果有人认为代码设计有问题,请与修改后的版本讨论 视图模型和模型代码 主要问题是如何使用嵌套列表类型属性Model.Students.States绑定下拉列表。 谢谢型号: namespace MyProject.Models { public class ViewInfo { public int StateID { get; set; } public in

这是我的全部代码。谁会看到它,然后他们就可以理解我正在努力实现的目标。在看过我的代码后,如果有人认为代码设计有问题,请与修改后的版本讨论

视图模型和模型代码 主要问题是如何使用嵌套列表类型属性
Model.Students.States
绑定下拉列表。 谢谢

型号:

namespace MyProject.Models
{
    public class ViewInfo
    {
        public int StateID { get; set; }
        public int? CityID { get; set; }
    }

    public class Student
    {
        public int ID { get; set; }
        public string FullName { get; set; }
        public int CityID { get; set; }
    }
    public class State
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    public Class City
    {
        public int ID { get; set; }
        public int StateID { get; set; }
        public string Name { get; set; }
    }
}
控制器:

using MyProject.Models;
namespace MyProject.Controllers
{
    public class StudentsController : Controller
    {
        private MyDBEntities _context;

        public StudentsController()
        {
            this._context = new MyDBEntities();
        }

        // StudentsController
        public ActionResult Index()
        {
            ViewBag.ViewInfo = new ViewInfo { StateID = 1 };
            ViewBag.StateID = _context.States.Select(s => new SelectListItem { Value = s.ID, Text = s.Name }).ToList();
            return View(_context.Students.ToList());
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(ViewInfo viewInfo)
        {
            ModelState.Clear();

            ViewBag.ViewInfo = viewInfo;
            ViewBag.StateID = _context.States.Select(s => new SelectListItem { Value = s.ID, Text = s.Name, Selected = s.ID == viewInfo.StateID }).ToList();
            ViewBag.CityID = _context.Cities.Select(s => new SelectListItem { Value = s.ID, Text = s.Name, Selected = s.ID == viewInfo.CityID ?? 1 }).ToList();

            var viewModel = _context.Students.Where(s => s.Cities.StateID == viewInfo.StateID && (viewInfo.CityID == null || s.CityID == viewInfo.CityID)).ToList();

            return View(viewModel);
        }
    }
}
以及以下观点:

@using MyProject.Models;
@model IEnumerable<MyProject.Models.Student>

@{
    ViewBag.Title = "Students";
    ViewInfo viewInfo = ViewBag.ViewInfo;
}

<div class="page-header">
    <h2>Students</h2>
</div>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken();

    @Html.DropDownList("StateID", null, new { @class = "form-control" })

    if(viewInfo.CityID != null){ @Html.DropDownList("CityID", null, new { @class = "form-control" }) }

    <input type="submit" value="Filter" class="btn btn-primary" />

    // Students Table
}
@使用MyProject.Models;
@模型IEnumerable
@{
ViewBag.Title=“学生”;
ViewInfo ViewInfo=ViewBag.ViewInfo;
}
学生
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken();
@DropDownList(“StateID”,null,new{@class=“form control”})
如果(viewInfo.CityID!=null){@Html.DropDownList(“CityID”,null,new{@class=“form control”}
//学生桌
}
型号:

namespace MyProject.Models
{
    public class ViewInfo
    {
        public int StateID { get; set; }
        public int? CityID { get; set; }
    }

    public class Student
    {
        public int ID { get; set; }
        public string FullName { get; set; }
        public int CityID { get; set; }
    }
    public class State
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    public Class City
    {
        public int ID { get; set; }
        public int StateID { get; set; }
        public string Name { get; set; }
    }
}
控制器:

using MyProject.Models;
namespace MyProject.Controllers
{
    public class StudentsController : Controller
    {
        private MyDBEntities _context;

        public StudentsController()
        {
            this._context = new MyDBEntities();
        }

        // StudentsController
        public ActionResult Index()
        {
            ViewBag.ViewInfo = new ViewInfo { StateID = 1 };
            ViewBag.StateID = _context.States.Select(s => new SelectListItem { Value = s.ID, Text = s.Name }).ToList();
            return View(_context.Students.ToList());
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(ViewInfo viewInfo)
        {
            ModelState.Clear();

            ViewBag.ViewInfo = viewInfo;
            ViewBag.StateID = _context.States.Select(s => new SelectListItem { Value = s.ID, Text = s.Name, Selected = s.ID == viewInfo.StateID }).ToList();
            ViewBag.CityID = _context.Cities.Select(s => new SelectListItem { Value = s.ID, Text = s.Name, Selected = s.ID == viewInfo.CityID ?? 1 }).ToList();

            var viewModel = _context.Students.Where(s => s.Cities.StateID == viewInfo.StateID && (viewInfo.CityID == null || s.CityID == viewInfo.CityID)).ToList();

            return View(viewModel);
        }
    }
}
以及以下观点:

@using MyProject.Models;
@model IEnumerable<MyProject.Models.Student>

@{
    ViewBag.Title = "Students";
    ViewInfo viewInfo = ViewBag.ViewInfo;
}

<div class="page-header">
    <h2>Students</h2>
</div>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken();

    @Html.DropDownList("StateID", null, new { @class = "form-control" })

    if(viewInfo.CityID != null){ @Html.DropDownList("CityID", null, new { @class = "form-control" }) }

    <input type="submit" value="Filter" class="btn btn-primary" />

    // Students Table
}
@使用MyProject.Models;
@模型IEnumerable
@{
ViewBag.Title=“学生”;
ViewInfo ViewInfo=ViewBag.ViewInfo;
}
学生
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken();
@DropDownList(“StateID”,null,new{@class=“form control”})
如果(viewInfo.CityID!=null){@Html.DropDownList(“CityID”,null,new{@class=“form control”}
//学生桌
}

我的问题已经解决。因此,我喜欢给出更新后的代码,因为相同的代码可能会帮助其他人

我的viewmodel和模型类
public类主视图模型
{
公共列表学生{get;set;}
public int SelectedState=0;
公共int SelectedCity=0;
}
公立班学生
{
公共int ID=0;
公共字符串名称=”;
公共int StateID=0;
公共int CityID=0;
公共列表状态{get;set;}
公共列表城市{get;set;}
}
公共阶级国家
{
公共int ID{get;set;}
公共字符串名称{get;set;}
}
公营城市
{
公共int ID{get;set;}
公共字符串名称{get;set;}
}
从中填充模型的控制器
公共类HomeController:控制器
{
公共行动结果索引()
{
MainViewModel oVm=新的MainViewModel()
{
学生=新名单(){
新生
{
ID=1,
Name=“JoyDev”,
StateID=1,
CityID=1,
状态=新列表()
{
新国家
{
ID=1,
Name=“西孟加拉邦”,
},
新国家
{
ID=2,
Name=“比哈尔邦”,
},
新国家
{
ID=3,
Name=“Orrisa”,
}
},
城市=新列表()
{
新城市
{
ID=1,
Name=“Alipur”
},
新城市
{
ID=2,
Name=“Asansol”
},
新城市
{
ID=3,
Name=“Andul”
}
}
},
//***********
新生
{
ID=1,
Name=“Mukti”,
StateID=2,
CityID=1,
状态=新列表()
{
新国家
{
ID=1,
Name=“西孟加拉邦”,
},
新国家
{
ID=2,
Name=“比哈尔邦”,
},
新国家
{
ID=3,
Name=“Orrisa”,
}
},
城市=新列表()
{
新城市
{
ID=1,
Name=“Janpur”
},
新城市
{
ID=2,
Name=“Madhubani”
},
新城市
{
ID=3,
Name=“Kanti”
}
}
},
//***********
新生
{
ID=1,
Name=“Somnath”,
StateID=3,
CityID=2,
状态=新列表()
{
新国家
{
ID=1,
Name=“西孟加拉邦”,
},
新国家
{
ID=2,
Name=“比哈尔邦”,
},
@using MyProject.Models;
@model IEnumerable<MyProject.Models.Student>

@{
    ViewBag.Title = "Students";
    ViewInfo viewInfo = ViewBag.ViewInfo;
}

<div class="page-header">
    <h2>Students</h2>
</div>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken();

    @Html.DropDownList("StateID", null, new { @class = "form-control" })

    if(viewInfo.CityID != null){ @Html.DropDownList("CityID", null, new { @class = "form-control" }) }

    <input type="submit" value="Filter" class="btn btn-primary" />

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

public class Student
{
    public int ID = 0;
    public string Name = "";
    public int StateID = 0;
    public int CityID = 0;
    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 class HomeController : Controller
    {
        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=1,
                        Name="Mukti",
                        StateID=2,
                        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="Janpur"
                            },
                            new Cities
                            {
                                ID=2,
                                Name="Madhubani"
                            },
                            new Cities
                            {
                                ID=3,
                                Name="Kanti"
                            }

                        }
                    },
//***********
                    new Student
                    {
                        ID=1,
                        Name="Somnath",
                        StateID=3,
                        CityID=2,
                        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="Chandapur"
                            },
                            new Cities
                            {
                                ID=2,
                                Name="Dhankauda"
                            },
                            new Cities
                            {
                                ID=3,
                                Name="Konarak"
                            }

                        }
                    }


                }

            };

            return View(oVm);
        }


    }
@model BuildTable.Models.MainViewModel
@{
    ViewBag.Title = "Home Page";
}

<div>
    <table>
    <tr>
        <td>ID</td>
        <td>Name</td>
        <td>State</td>
        <td>City</td>
    </tr>
        @for(int i = 0; i < Model.Students.Count; i++)
        {
            <tr>
                <td><input type="text" value="@Model.Students[i].ID" /></td>
                <td><input type="text" value="@Model.Students[i].Name" /></td>
                <td>
                    @Html.DropDownListFor(m => m.Students[i].StateID, new SelectList(Model.Students[i].States, "ID", "Name",Model.Students[i].StateID), "-- Select States--", new { @class = "edit-mode" })
                </td>
                <td>
                    @Html.DropDownListFor(m => m.Students[i].CityID, new SelectList(Model.Students[i].Cities, "ID", "Name",Model.Students[i].CityID), "-- Select Cities--", new { @class = "edit-mode" })
                </td>
            </tr>
        }
    </table>
</div>