MVC3。如何使用此方法检索House Entities内的数据?

时间:2012-04-11 06:55:23

标签: c# nhibernate

public IList<House> GetFullList()
        {
            try
            {
                var session = SessionFactory.GetCurrentSession();

                return session.QueryOver<House>()
                    .OrderBy(x => x.Owner).Asc
                    .List<House>();  

            }
            catch (NHibernate.ADOException nex)
            {
                log.Error(System.Reflection.MethodBase.GetCurrentMethod().Name + " Error-- " + nex.ToString());
                throw new Exception(ExceptionConstants.ERR_DATABASE_ERROR);
            }
        }

上面的代码在“PostService.css”上。现在,我试图使用该方法将我的所有数据从数据库检索到名为AllHouses()的视图中。众议院的实体包括。所有者,价格,位置和描述。如何将我的数据导入GridView。我想制作“编辑”“删除”功能..

1 个答案:

答案 0 :(得分:1)

只需遍历方法返回的列表即可。所以,如果你在一个动作中调用它:

// This code is in your controller

using  TemplateProject.Core.Entities;
// ...

public ActionResult HouseList()
{
   // call method to retrieve the list of houses
   var service = new PostService();
   var houses = service.GetFullList();

   // this will render the view called "HouseList", passing it the 
   // list of houses as view model
   return View(houses)
}

HouseList视图(HouseList.cshtml)中,您可以进行迭代 房屋清单并将其呈现在一张桌子中:

@using TemplateProject.Core.Entities
@model IEnumerable<House>

<table>
   <thead>
      <th>Description</th>
      <th>Description</th>
   </thead>
   <tbody>
      @foreach(var house in Model)
      {
         <tr><td>@house.Description</td></tr>
         <tr><td>@house.Price</td></tr>
      }
   </tbody>
</table>

如果您有网格控件,则可以使用视图的Model属性进行构建。