ASP.NET MVC : How to bind dropdown list with neste

2019-08-26 02:27发布

here is my full code. who will see it then they could understand what i am trying to achieve. after seeing my code if anyone think there is problem in code design then please discuss with rectified version.

view model and model code

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 = 0;
    public string Name = "";
}

public class Cities
{
    public int ID = 0;
    public string Name = "";
}

Controller code from where i am populating my view model and model

    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();
        }

view code where i was trying to bind drop down.

@model WebApplication1.Models.MainViewModel
@{
    ViewBag.Title = "Home Page";
}
<div>
    <table>
        <tr>
            <td>ID</td>
            <td>Name</td>
            <td>State</td>
            <td>City</td>
        </tr>
        @foreach (var m in Model.Students)
        {
            <tr>
                <td><input type="text" value="@m.ID" /></td>
                <td><input type="text" value="@m.Name" /></td>
                <td>
                    @Html.DropDownList("CityID", new SelectList(ViewData["CityList"] as List<SelectListItem>, "Value", "Text", m.CityID))
                    @Html.DropDownListFor(x => x.SelectedState new SelectList(Model.Students.States, "ID", "Name", Model.SelectedState), "-- Select States--", new { id = "cboState", @class = "edit-mode" })
                </td>
                <td>
                    @Html.DropDownListFor(x => x.SelectedCity new SelectList(Model.Students.Cities, "ID", "Name", Model.SelectedCity), "-- Select States--", new { id = "cboState", @class = "edit-mode" })
                </td>
            </tr>
        }
    </table>
</div>

main problem lies here

@Html.DropDownListFor(x => x.SelectedState new SelectList(Model.Students.States, "ID", "Name", Model.SelectedState), "-- Select States--", new { id = "cboState", @class = "edit-mode" })

main question is how to bind drop down with nested list type property Model.Students.States. thanks

2条回答
对你真心纯属浪费
2楼-- · 2019-08-26 03:20

Models:

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; }
    }
}

Controllers:

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);
        }
    }
}

And the View:

@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
}
查看更多
手持菜刀,她持情操
3楼-- · 2019-08-26 03:29

My issue has been solved. so i like to give my updated code because the same code may help other.

my viewmodel and model classes

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; }
}

Controller from where i populate my model

    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);
        }


    }

view code

@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>

thanks

查看更多
登录 后发表回答