【问题标题】:Binding to a DropDownList in a ASP.NET MVC4 using Nhibernate使用 Nhibernate 绑定到 ASP.NET MVC4 中的 DropDownList
【发布时间】:2014-10-20 10:54:19
【问题描述】:

我是 ASP.NET MVC 和 Nhibernate 的新手

我遇到了这个要求,我必须搜索一个表格以在同一页面中显示数据。 用户应该能够通过四个输入进行搜索。姓名、身份证、出生日期和城市。应键入前三个,并应从下拉列表中选择城市,该下拉列表由城市列的不同值填充。

我已经开发了一个级别,用户可以从姓名、ID、出生日期进行搜索。但是我在将值绑定到下拉列表时遇到了麻烦。

谁能帮帮我。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 nhibernate asp.net-4.0


    【解决方案1】:

    好的,我找到了一种方法,但我不确定这是否是最好的方法。

    一个新的模型类

    public class Countries
    {
        public virtual string citizenship { get; set; }
    }
    

    在控制器中,在我的情况下,在 get 和 post 中

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多