OhSnap
OhSnap

Reputation: 376

MVC 4 View doesnt recognize model

[EDIT]

@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>

I just started working with MVC/NHibernate today by going through a little tutorial. Everything went fine until I tried to create a view out of my model through an ActionResult (Index). It seems that the view cant find the model and as I'm literally a Beginner I have no clue what to do.

Could you guys have a look at my code and tell me what I did wrong or at least give a clue?

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>

Upvotes: 0

Views: 1956

Answers (2)

Corey Cole
Corey Cole

Reputation: 987

You're sending an IList into your view. This will display a single item.

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

If you really want a list (e.g., you're going to display a table or some such), keep your action as is and change your view to:

@model IList<LocateIt.ModelsLocationInfo>

Upvotes: 1

Oskar Berggren
Oskar Berggren

Reputation: 5629

I don't understand what the parentheses are doing in the model declaration. The syntax for @model should be:

@model Your.Namespace.ClassName

The in your code you use Model, not model.

Upvotes: 0

Related Questions