[编辑]
@model LocationInfo (LocateIt.Models)
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
</div>
</body>
</html>
我今天刚通过一个小教程开始使用 MVC/NHibernate。一切都很顺利,直到我尝试通过 ActionResult(索引)从我的模型中创建一个视图。视图似乎找不到模型,因为我实际上是初学者,所以我不知道该怎么做。
你们能看看我的代码并告诉我我做错了什么或者至少给出一个线索吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LocateIt.Models
{
public class LocationInfo
{
public virtual int Id { get; set; }
public virtual string LocationName { get; set; }
public virtual string LocationDescription { get; set; }
public virtual string City { get; set; }
public virtual string Street { get; set; }
public virtual string HouseNumber { get; set; }
public virtual short PLZ { get; set; }
public virtual decimal Rating { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LocateIt.Models;
using LocateIt.Models.NHibernate;
namespace LocateIt.Controllers
{
public class LocationInfoController : Controller
{
LocationInfoRepository _repository;
public LocationInfoController()
{
_repository = new LocationInfoRepository();
}
public ActionResult Index()
{
IList<LocationInfo> LocationInfo = _repository.GetLocation("Oberhausen");
return View(LocationInfo);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate;
using NHibernate.Linq;
namespace LocateIt.Models.NHibernate
{
public class LocationInfoRepository
{
public IList<LocationInfo> GetLocation(string city)
{
using (ISession session = NHibernateHelper.OpenSession())
{
return session.Query<LocationInfo>().ToList();
}
}
public void Save(LocationInfo objLocationInfo)
{
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(objLocationInfo);
transaction.Commit();
}
}
}
}
}
using NHibernate;
using NHibernate.Cfg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LocateIt.Models.NHibernate
{
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var NHibernateConfig = new Configuration();
NHibernateConfig.Configure(HttpContext.Current.Server.MapPath(
@"Models\NHibernate\Configuration\hibernate.cfg.xml"));
NHibernateConfig.AddDirectory(new System.IO.DirectoryInfo(
HttpContext.Current.Server.MapPath(@"Models\NHibernate\Mappings")));
_sessionFactory = NHibernateConfig.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2"
assembly="MVC4_Using_NHB"
namespace="MVC4_Using_NHB"
auto-import="true">
<class name="MVC4_Using_NHB.Models.LocationInfo,MVC4_Using_NHB">
<id name="Id" access="property" column="Id" type="Int32">
<generator class="native"></generator>
</id>
<property name="LocationName" access="property"
column="LocationName" type="String"></property>
<property name="LocationDescription" access="property"
column="LocationDescription" type="String"></property>
<property name="City" access="property"
column="City" type="String"></property>
<property name="Street" access="property"
column="Street" type="String"></property>
<property name="HouseNumber" access="property"
column="HouseNumber" type="String"></property>
<property name="PLZ" access="property"
column="PLZ" type="Int16"></property>
<property name="Rating" access="property"
column="Rating" type="Int32"></property>
</class>
</hibernate-mapping>