ASP.Net MVC:使用嵌套列表类型集合绑定下拉列表

时间:2016-01-10 18:11:27

标签: asp.net-mvc asp.net-mvc-4 webgrid dropdownlistfor

我在webgrid中显示学生数据。我正在显示学生ID,名字,姓氏,州和城市。我现在正在工作的方式我正在获取数据。我正在进行行编辑的webgrid工作。当我点击编辑按钮然后文本框和下拉列表在网格单元格上正确显示。现在州和城市的下降即将到来。我现在编码的方式状态显示所有州和城市也显示所有城市,但我希望州下拉列表应显示所有州,但城市下拉列表应该只显示基于学生州身份的城市。

现在在我的视图模型状态和城市列表类型属性独立加载,但我想重新设计代码。我想加载所有状态但是在城市的情况下我想根据学生状态id加载它。

现在我的预设视图模型看起来像

public class StudentListViewModel
{
    public int StartIndex { get; set; }
    public int EndIndex { get; set; }
    public int page { get; set; }
    public int RowCount { get; set; }
    public int PageSize { get; set; }
    public int CurrentPage { get; set; }

    public string sort { get; set; }
    public string sortdir { get; set; }

    public IList<Student> Students { get; set; }

    public int SelectedStateId { set; get; }
    public IList<State> States { get; set; }

    public int SelectedCityId { set; get; }
    public IList<City> Cities { get; set; }

    public StudentListViewModel()
    {
        PageSize = 5;
        sort = "ID";
        sortdir = "ASC";
        CurrentPage = 1;
    }

    public void SetUpParams(StudentListViewModel oSVm)
    {
        if (oSVm.page == 0)
            oSVm.page = 1;

        StartIndex = ((oSVm.page * oSVm.PageSize) - oSVm.PageSize) + 1;
        EndIndex = (oSVm.page * oSVm.PageSize);
        CurrentPage = (StartIndex - 1) / oSVm.PageSize;

        if (string.IsNullOrEmpty(oSVm.sort))
            oSVm.sort = "ID";

        if (string.IsNullOrEmpty(oSVm.sortdir))
            oSVm.sortdir = "ASC";
    }
}

模型类

   public class Student
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public bool IsActive { get; set; }
        public int StateID { get; set; }
        public string StateName { get; set; }
        public int CityID { get; set; }
        public string CityName { get; set; }
    }

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

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

Student controller
---------------------
  public class StudentController : Controller
    {
        private StudentRepository _Studentdata;
        private StateRepository _Statedata;
        private CityRepository _Citydata;

        public StudentController()
        {

            _Studentdata = new StudentRepository(System.Configuration.ConfigurationManager.ConnectionStrings["StudentDBContext"].ConnectionString);
            _Statedata = new StateRepository(System.Configuration.ConfigurationManager.ConnectionStrings["StudentDBContext"].ConnectionString);
            _Citydata = new CityRepository(System.Configuration.ConfigurationManager.ConnectionStrings["StudentDBContext"].ConnectionString);
        }

        // GET: Stuent
        public ActionResult List(StudentListViewModel oSVm)
        {
            if (Request.IsAjaxRequest())
                System.Threading.Thread.Sleep(1000); // just simulate delay of one second

            StudentListViewModel SVm = new StudentListViewModel();
            SVm.SetUpParams(oSVm);
            SVm.Students = _Studentdata.GetStudents(oSVm.page, oSVm.PageSize, oSVm.sort, oSVm.sortdir).ToList(); // populating student
            SVm.States = _Statedata.GetAll().ToList(); // populating state
            SVm.Cities = _Citydata.GetAll().ToList(); // populating city
            SVm.RowCount = _Studentdata.DataCounter;
            return View("ListStudents",SVm);
        }

}

这是我的webgrid代码

@{
    WebGrid grid = new WebGrid(null, rowsPerPage: Model.PageSize, canPage: true,
        defaultSort: Model.sort, ajaxUpdateContainerId: "gridContent", ajaxUpdateCallback: "initScripts");
    grid.Bind(Model.Students, autoSortAndPage: false, rowCount: Model.RowCount);
    grid.PageIndex = Model.CurrentPage;
}


@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 <input type="hidden" name="HiddenID" value="@item.ID" id="HiddenID"  /></text>,style:"SmallCols"),
            grid.Column(columnName: "FirstName", header: "First Name", format: @<text><span class="display-mode">@item.FirstName</span><input type="text" id="txtFirstName" value="@item.FirstName" class="edit-mode" /></text>,style:"NameColWidth" ),
            grid.Column(columnName: "LastName", header: "Last Name", format: @<text><span class="display-mode">@item.LastName</span><input type="text" id="txtLastName" value="@item.LastName" class="edit-mode" /></text>,style:"NameColWidth"),

            grid.Column(columnName: "StateName", header: "State Name", format: @<text><span class="display-mode">@item.StateName</span>@Html.DropDownListFor(x => x.SelectedStateId, new SelectList(Model.States, "ID", "Name", Model.SelectedStateId= item.StateID), "-- Select States--", new { id = "cboState", @class = "edit-mode" })</text>,style:"NameColWidth"),
            grid.Column(columnName: "CityName", header: "City Name", format: @<text><span class="display-mode">@item.CityName</span>@Html.DropDownListFor(x => x.SelectedCityId, new SelectList(Model.Cities, "ID", "Name", Model.SelectedCityId = item.CityID), "-- Select City--", new { id = "cboCity", @class = "edit-mode" })
</text>,style:"NameColWidth"),
            grid.Column(header: "IsActive",
            format: @<text><input id="select" class="box" name="select"
                                  type="checkbox" @(item.IsActive ? "checked='checked'" : "") value="@item.IsActive" /></text>
                    , style: "text-center checkbox-width SmallCols"),

             grid.Column("Action", format: @<text>
                        <button class="edit-user display-mode btnGreen">Edit</button>
                        <button class="edit-user display-mode btnRed">Delete</button>
                        <button class="save-user edit-mode btnGreen">Save</button>
                        <button class="cancel-user edit-mode btnSky">Cancel</button>
            </text>, style: "ActionCol", canSort: false)

                        ))

我可以将此属性public IList<City> Cities { get; set; }从视图模型放到学生类但问题是我不知道如何在下拉列表中引用嵌套列表类型属性

这样我的下拉列表可以指向嵌套的Cities属性吗?

@Html.DropDownListFor(x => x.SelectedCityId, new SelectList(Model.Cities, "ID", "Name", Model.SelectedCityId = item.CityID), "-- Select City--", new { id = "cboCity", @class = "edit-mode" })

how to refer this line Model.Students.Cities i mean
@Html.DropDownListFor(x => x.SelectedCityId, new SelectList(Model.Students.Cities, "ID", "Name", Model.SelectedCityId = item.CityID), "-- Select City--", new { id = "cboCity", @class = "edit-mode" })

我试过了Model.Students.Cities但是城市财产还没来!

我犯了错误?寻求指导和建议。感谢

0 个答案:

没有答案