2010-02-25 51 views
0

我是新来的ASP.NET MVC,我正在创建一个应用程序,使用自动完成功能和jQuery一起搜索联系人。ASP.NET MVC令人讨厌的错误 - 无法找到类型或命名空间名称

当我运行该项目时,它加载正常,当我点击提交按钮来搜索姓氏,我收到一个错误。

Server Error in '/' Application.
The resource cannot be found

Requested URL:/Offender/Search

在尝试这个项目我也跟着一起witht罗斯文MVC样品和项目也给了我的错误,以及。

任何帮助表示赞赏!是感谢

我接收的误差如下:

错误1个

The type or namespace name 'KOOLSModel' could not be found (are you missing a using directive or an assembly reference?)
C:\Documents and Settings\My Documents\Visual Studio 2008\Projects\DOC_KOOLS\DOC_KOOLS\Controllers\OffenderController.cs 6 7 DOC_KOOLS

错误2

The type or namespace name 'KOOLSEntities' could not be found (are you missing a using directive or an assembly reference?)
C:\Documents and Settings\My Documents\Visual Studio 2008\Projects\DOC_KOOLS\DOC_KOOLS\Controllers\OffenderController.cs 15 32 DOC_KOOLS

错误3

The type or namespace name 'Offender' could not be found (are you missing a using directive or an assembly reference?)
C:\Documents and Settings\My Documents\Visual Studio 2008\Projects\DOC_KOOLS\DOC_KOOLS\Controllers\OffenderController.cs 32 22 DOC_KOOLS

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Web; 

using System.Web.Mvc; 

using KOOLSModel; 

namespace DOC_KOOLS.Controllers 
{ 
    public class OffenderController : Controller 
    { 
     // 
     // GET: /Offender/ 
     //KOOLSEntities KOOLS = new KOOLSEntities(); 
     KOOLSEntities db = new KOOLSEntities(); 

     public ActionResult Index() 
     { 
      ViewData["Message"] = "Welcome to KOOL!"; 
      return View(); 
     } 

     public ActionResult getAjaxResult(string q) 
     { 
      string searchResult = string.Empty; 

      var offender = (from o in db.Offender 
          where o.Lastname.Contains(q) 
          orderby o.LastName 
          select o).Take(10); 

      foreach (Offender o in offender) 
      { 
       searchResult += string.Format("{0}|\r\n", o.LastName); 
      } 
      return Content(searchResult); 
     } 

     [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Search(string searchTerm) 
     { 
      if (searchTerm == string.Empty) 
      { 
       return View(); 
      } 
      else 
      { 
       // if the search contains only one result retunr details 
       // otherwise a list 
       var offenders = from o in db.Offender 
           where o.LastName.Contains(searchTerm) 
           orderby o.LastName 
           select o; 
       if (offenders.Count() == 0) 
       { 
        return View("notfound"); 
       } 

       if (offenders.Count > 1) 
       { 
        return View("List", offenders); 
       } 
       else 
       { 
        return RedirectToAction("Details", new { id = offenders.First().sPN }); 
       } 
      } 
     } 
     public ActionResult About() 
     { 
      return View(); 
     } 
    } 
} 
+0

好像你的错误是编译错误,而不是运行时错误,但你说应用程序启动正常。我期望这些类型的错误,当框架动态编译视图,但不是在Web应用程序启动之前应该编译的控制器中。您的解决方案中有多少个项目,它们是如何分裂的? – NerdFury 2010-02-25 15:37:51

+0

我的解决方案中只有一个项目。 – 2010-02-25 18:29:09

回答

1

看起来您并未添加对包含实体模型的程序集的引用。

+0

我刚刚添加了System.Data&System.Data.EntityModel,它仍然给我错误“KOOLSModel”和“KOOLSEntities”的下面一行“KOOLSEntities db = new KOOLSEntities();” – 2010-02-25 16:29:02

+0

我需要什么参考加? – 2010-02-25 18:31:25

1

你是否已经添加了对web.config的引用?如果您有任何意见都强类型,那么你将需要添加一个节在你的web.config的“页”节

<pages> 
    ... 
    <add namespace="KOOLSModel"/> 
    <add namespace="System.Web.Mvc"/> 
    .... 
+0

不,我有没有关系,如果我把它添加到web.config在视图级别或根级别 – 2010-02-25 18:34:22

+0

我得到一个配置错误,当我运行它。 已存在于web.config中 – 2010-02-25 18:40:23

+0

将其放入您站点的根web.config中。不是在视图文件夹中的配置。 – David 2010-02-26 06:58:15

相关问题