[编辑]
@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>
答案 0 :(得分:1)
您正在向您的视图发送IList。这将显示单个项目。
public ActionResult Index()
{
var info = _repository.GetLocation("Oberhausen").First();
return View(info);
}
如果您真的想要一个列表(例如,您要显示一个表格或某些表格),请按原样保持您的操作,并将您的观点更改为:
@model IList<LocateIt.ModelsLocationInfo>
答案 1 :(得分:0)
我不明白括号在模型声明中做了什么。 @model的语法应为:
@model Your.Namespace.ClassName
您的代码中使用的是Model
,而不是model
。