1

Im new to ASP.NET MVC3. I got stuck with this error, while I was doing the same thing as here in these videos:

http://tv.telerik.com/watch/orm/building-a-mvc-3-application-database-first-with-openaccess-creating-model?seriesID=1529 and

http://tv.telerik.com/watch/orm/building-a-mvc-3-application-database-first-with-openaccess-creating-controllers

(i looked other similar questions on stack but didnt find the solution)

my code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace testbaza.Controllers
{
    public class KorController : Controller
    {

        private EntitiesModel dbContext;
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
            if (this.Session[ContextModule.Content_KEY] != null)
            {
                this.dbContext = this.Session[ContextModule.Content_KEY] as EntitiesModel;
            }
            else {
                throw new Telerik.OpenAccess.Exceptions.NoSuchObjectException("Cannot find EntitiesModel", null);
            }
        }

and im getting this error: The name 'ContextModule' does not exist in the current context.

This is mine further code which i did before:

I added this in project\Web.config(same as in video 1):

 <httpModules>
      <add name="ContextModule" type="testbaza.ContextModule, testbaza"/>
    </httpModules>

I added ASP.NET module called "ContextModule" to \project(same as in video)

This is ContextModule.cs:

using System;
using System.Web;

namespace testbaza.Models
{
    public class ContextModule : IHttpModule
    {

        internal const string CONTEXT_KEY = "datacontext";

        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute);
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }

        private void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session != null)
            {
                HttpContext.Current.Session[CONTEXT_KEY] = new EntitiesModel();
            }
        }

        private void context_PostRequestHandlerExecute(object sender, EventArgs e)
        {
            CommitTransactions();

            DisposeContext();

            ClearSession();

        }

        private void CommitTransactions()
        {
            if (HttpContext.Current.Session == null)
            {
                return;
            }

            EntitiesModel dbContext =
                HttpContext.Current.Session[CONTEXT_KEY] as EntitiesModel;
            if (dbContext != null)
            {

                dbContext.SaveChanges();
            }
        }

        private void DisposeContext()
        {
            if (HttpContext.Current.Session == null)
            {
                return;
            }

            EntitiesModel dbContext =
                HttpContext.Current.Session[CONTEXT_KEY] as EntitiesModel;
            if (dbContext != null)
            {

                dbContext.Dispose();
            }
        }

        private void ClearSession()
        {

            if (HttpContext.Current.Session == null)
            {
                HttpContext.Current.Session.Remove(CONTEXT_KEY);
            }
        }
    }
}

Can anyone help me with the problem? Thanks in advance!

4

2 回答 2

3

在您的控制器中添加以下内容:

using testbaza.Models;

你应该没事。

希望这可以帮助

于 2012-09-20T01:04:58.877 回答
2

该类是在testbaza.Models名称空间中定义的,而您的控制器不在其中。
您需要使用using语句导入该名称空间。

于 2012-09-19T23:38:19.757 回答