2012-03-20 189 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項目列表。
  • 第三數據值字段(主鍵)
  • 最後一個是您想要在下拉列表中顯示的數據文本字段。