2013-06-22 104 views
0

[編輯]MVC 4查看犯規識別模型

@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(Index)創建出我的模型的視圖。看起來這個視圖不能找到這個模型,因爲我真的是一個初學者,我不知道該怎麼做。

難道你們看看我的代碼,並告訴我我做錯了什麼或者至少給了一個線索?

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

什麼是「它似乎不能找到模型「的意思?你有沒有編譯錯誤或運行時異常?發生了什麼(或者什麼都沒有發生)讓你認爲有什麼不對? – nemesv

+0

我得到錯誤:視圖內部的「名稱」模型「在當前上下文中不存在」。 – OhSnap

+0

你可能弄亂了視圖。你發佈的視圖看起來像一個佈局,但它似乎像你試圖使用它像一個視圖。那個文件的名字是什麼? –

回答

0

我不明白括號在模型聲明中做了什麼。對於@model的語法應爲:

@model Your.Namespace.ClassName 

的在你的代碼中使用Model,不model

1

您正在將IList發送到您的視圖中。這將顯示一個項目。

public ActionResult Index() 
{ 
    var info = _repository.GetLocation("Oberhausen").First(); 
    return View(info); 
} 

如果你真的想要一個列表(例如,你要顯示一個表或一些這樣的),讓你的行動是,改變你的觀點:

@model IList<LocateIt.ModelsLocationInfo>