ASP.NET MVC:如何绑定嵌套列表type属性下拉列表 [英] ASP.NET MVC : How to bind dropdown list with nested list type property
本文介绍了ASP.NET MVC:如何绑定嵌套列表type属性下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的全code。谁将会看到它,然后他们能明白我想要的目的。看到我的code。如果有人认为后没有在code设计问题,那么请与整流后的版本讨论。
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.
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 = "";
}
从我在哪里我的填充视图模型和
控制器code模型
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();
}
查看code,其中我试图绑定掉了下来。
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>
的主要问题就在这里
@Html.DropDownListFor(x => x.SelectedState new SelectList(Model.Students.States, "ID", "Name", Model.SelectedState), "-- Select States--", new { id = "cboState", @class = "edit-mode" })
主要问题是如何绑定下拉嵌套列表type属性 Model.Students.States
。
谢谢
推荐答案
我的问题已经解决了。所以我想给我的更新code,因为同样的code可以帮助其他。
My issue has been solved. so i like to give my updated code because the same code may help other.
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);
}
}
查看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>
感谢
这篇关于ASP.NET MVC:如何绑定嵌套列表type属性下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文