2012-03-20 101 views
0

我在hibernate中创建了一个应用程序,我需要在创建视图中创建一个下拉列表。在MVC中创建下拉列表nhibernate

下拉列表项是通过名为getHobbytype()的函数获取的,我需要将所选值存储到不同的数据库中。在我创建视图

ViewData["Hobby_type"] = 
     new SelectList(new Hobby_MasterService().GetHobbyType(),"Hobby_Types"); 

这:

我已经在我的控制器写了这

@Html.DropDownListFor(Model => 
     Model.Hobby_Types,(IEnumerable<SelectListItem>)ViewData["Hobby_type"]) 

通过这次我能够创建下拉列表,但它给我这个我在下拉列表中查看错误:

没有类型为“IEnumerable”的ViewData项目具有“Hobby_Types”键。

这里是我的GetHobbyType方法:

public IList<String> GetHobbyType() 
{ 
    log.Debug("Started"); 
    ISession session = DataAccessLayerHelper.OpenReaderSession(); 
    IList<String> htype = null; 
    ITransaction transaction = null; 
    try 
    { 
    transaction = session.BeginTransaction(); 
    htype = session.CreateSQLQuery("SELECT Hobby_Types FROM Hobby_Type").List<String>(); 
    session.Flush(); 
transaction.Commit(); 
} 
    catch (Exception ex) 
{ 
    if (transaction != null && transaction.IsActive) 
    transaction.Rollback(); 
    log.Error(ex); 

    } 
    log.Debug("End"); 
    return htype; 
    } 

请告诉我,我要去哪里错了。

+0

ViewData [“Type”]我想你的意思是ViewData [“Hobby_Type”] – Iridio 2012-03-20 06:19:23

+0

我更新了问题 – user1274646 2012-03-20 06:52:58

+0

你也可以发布'GetHobbyType'方法吗? – Rippo 2012-03-20 07:46:39

回答

1

这是一个错字: -

@Html.DropDownListFor(Model => 
     Model.Hobby_Types,(IEnumerable<SelectListItem>)ViewData["Type"]) 

岂不是

@Html.DropDownListFor(Model => 
     Model.Hobby_Types,(IEnumerable<SelectListItem>)ViewData["Hobby_type"]) 

而且你的错误说:'IEnumerable' that has the key 'Hobby_Types'.

在ViewData的关键是区分大小写(更不用说该错误在最后有一个S)

我也会推荐使用ViewModel而不是ViewData。看到这个Google搜索

编辑GetHobbyType方法返回一个列表,以便尝试这个办法: -

ViewData["Hobby_type"] =  
    new SelectList(
    new Hobby_MasterService().GetHobbyType() 
    .Select(x => new SelectListItem { Text = x, Value = x }).ToList() 
    ,"Hobby_Types"); 

我还建议在寻找使用视图模型,因为这将节省您大量的头痛!

+0

yah它的Hobby_Type ...但我不想创建视图模型....你能告诉我任何没有它的方法 – user1274646 2012-03-20 06:52:37

+0

它应该是'Hobby_type'? – Rippo 2012-03-20 07:01:07

+0

好了,但仍然得到相同的错误。 – user1274646 2012-03-20 07:07:52

-1

你可以试试这一切。

你必须写一个服务named GetAllStudents()

public IList<Student> GetAllStudents() 
{ 

    log.Debug("Started"); 
    ISession session = DataAccessLayerHelper.OpenReaderSession(); 
    IList<Student> students = null; 
    ITransaction transaction = null; 
    try 
    { 
     transaction = session.BeginTransaction(); 

     ICriteria criteria = session.CreateCriteria(typeof(Student)); 
     students = criteria.List<Student>(); 
     session.Flush(); 
     transaction.Commit(); 
    } 
    catch (Exception ex) 
    { 
     if (transaction != null && transaction.IsActive) 
      transaction.Rollback(); 
     log.Error(ex); 

    } 
    finally 
    { 
     if (transaction != null) 
      transaction.Dispose(); 

     if (session != null && session.IsConnected) 
      session.Close(); 
    } 

    log.Debug("End"); 
    return students; 
} 

在控制器:

ViewBag.std = new StudentService().GetAllStudents(); // ViewBag.std will hold all students. 

在创建视图:

@Html.DropDownListFor(model => model.Name, new SelectList(ViewBag.std, "Id", "Name"), "-- Select --") 
  • 第一个参数是负责LINQ表达式类你想要的财产放置在下拉菜单中。
  • 第二个是IEnumerable项目列表。
  • 第三数据值字段(主键)
  • 最后一个是您想要在下拉列表中显示的数据文本字段。