【发布时间】:2012-03-20 04:59:49
【问题描述】:
我正在休眠中创建一个应用程序,我需要在我的创建视图中创建一个下拉列表。
下拉列表项是通过一个名为 getHobbytype() 的函数获取的,我需要将选定的值存储到不同的数据库中。
我已经在我的控制器中写了这个:
ViewData["Hobby_type"] =
new SelectList(new Hobby_MasterService().GetHobbyType(),"Hobby_Types");
这在我的创建视图中:
@Html.DropDownListFor(Model =>
Model.Hobby_Types,(IEnumerable<SelectListItem>)ViewData["Hobby_type"])
通过这个我能够创建下拉列表,但它在我的下拉视图中给了我这个错误:
没有具有键“Hobby_Types”的“IEnumerable”类型的 ViewData 项。
这是我的 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;
}
请告诉我哪里出错了。
【问题讨论】:
-
ViewData["Type"] 我想你的意思是 ViewData["Hobby_Type"]
-
能否也发一下
GetHobbyType的方法?
标签: asp.net-mvc nhibernate drop-down-menu