Question

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!

Était-ce utile?

La solution

In your controller add the following:

using testbaza.Models;

and you should be OK.

Hope this helps

Autres conseils

That class is defined in the testbaza.Models namespace, which your controller is not in.
You need to import that namespace using a using statement.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top