【问题标题】:Creating a dropdown list in MVC nhibernate在 MVC nhibernate 中创建下拉列表
【发布时间】: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


【解决方案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搜索

edit GetHobbyType 方法返回一个列表,所以试试这个:-

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

我还建议考虑使用视图模型,因为它会为您省去很多麻烦!

【讨论】:

  • 是的 Hobby_Type ......但我不想创建视图模型......你能告诉我没有它的任何方法
  • 当您的方法返回列表时发布的编辑
【解决方案2】:

你可以试试这一切。

你要写一个服务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项目列表。
  • 第三个数据值字段。(主键)
  • 最后一个是要在下拉列表中显示的数据文本字段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多