该名称在当前上下文中不存在

时间:2012-09-19 23:31:27

标签: asp.net-mvc asp.net-mvc-3 telerik telerik-mvc telerik-open-access

我是ASP.NET MVC3的新手。我遇到了这个错误,而我在这些视频中也做了同样的事情:

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

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

(我在堆栈上看了其他类似的问题,但没有找到解决方案)

我的代码是:

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);
            }
        }

我发现此错误:当前上下文中不存在名称“ContextModule”。

这是我之前做的进一步代码:

我在project \ Web.config中添加了这个(与视频1中相同):

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

我将名为“ContextModule”的ASP.NET模块添加到\ project(与视频相同)

这是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);
            }
        }
    }
}

任何人都可以帮我解决这个问题吗? 提前谢谢!

2 个答案:

答案 0 :(得分:3)

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

using testbaza.Models;

你应该没事。

希望这有帮助

答案 1 :(得分:2)

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