使用Nhibernate绑定到ASP.NET MVC4中的DropDownList
本文关键字:MVC4 中的 DropDownList NET ASP Nhibernate 绑定 使用 | 更新日期: 2023-09-27 18:27:19
我是ASP.NET MVC和Nhibernate 的新手
我遇到了这个要求,我必须在同一页中搜索一个表来显示数据。用户应该能够通过四个输入进行搜索。姓名、身份证、出生日期和城市。前三个应该输入,城市应该从下拉列表中选择,该下拉列表由城市的不同值填充。
我已经发展到一个用户可以从姓名,身份证,出生日期搜索的水平。但我在将值绑定到下拉列表时遇到了麻烦。
有人能帮我一下吗。
好的,我找到了一种方法,但我不确定这是否是最好的方法。
一种新型型号
public class Countries
{
public virtual string citizenship { get; set; }
}
在控制器中,在我的情况下,在获取和发布中
TDAL tda = new TDAL();
//////////////////////
List<SelectListItem> list= new List<SelectListItem>();
list.Add(new SelectListItem { Text = "All", Value = "0" });
var cat = tda.getCountries().ToArray();
for (int i = 0; i < cat.Length; i++)
{
list.Add(new SelectListItem
{
Text = cat[i].citizenship,
Value = cat[i].citizenship.ToString(),
Selected = (cat[i].citizenship == "1")
});
}
ViewData["citizen"] = list;
////////////////////////////
return View();
DAL类
public IList<Countries> getCountries()
{
IList<Ter.Models.Countries> countries;
IQuery query;
using (ISession session = OpenSession())
{
try
{
query = session
.CreateQuery("select distinct citizenship AS citizenship from Ters")
.SetResultTransformer(Transformers.AliasToBean<Countries>());
}
catch (Exception c)
{
query = null;
}
countries = query.List<Countries>();
}
return countries;
}
和视图
@Html.DropDownList("citizenship", (IEnumerable<SelectListItem>)ViewData["citizen"], new { id = "citizenship" })