2010-03-12 440 views
1

我是ASP.NET MVC 2的新手。 我不明白为什么我收到此错误。是否有遗漏,我没有正确引用。'/'应用程序中的服务器错误。 - 资源无法找到

我试图创建一个简单的jQuery自动完成网上搜索文本框,并查看我选择的人的细节

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Mvc.Ajax; 
using DOC_Kools.Models; 

namespace DOC_Kools.Controllers 
{ 
    public class HomeController : Controller 
    { 
     private KOOLSEntities _dataModel = new KOOLSEntities(); 

     // 
     // GET: /Home/ 

     public ActionResult Index() 
     { 
      ViewData["Message"] = "Welcome to ASP.NET MVC!"; 

      return View(); 

     } 

     // 
     // GET: /Home/ 

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

      var offenders = (from o in _dataModel.OffenderSet 
          where o.LastName.Contains(q) 
          orderby o.LastName 
          select o).Take(10); 

      foreach (Offender o in offenders) 
      { 
       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 return detials 
       // otherwise a list 
       var offenders = from o in _dataModel.OffenderSet 
           where o.LastName.Contains(searchTerm) 
           orderby o.LastName 
           select o; 

       if (offenders.Count() == 0) 
       { 
        return View("not found"); 
       } 

       if (offenders.Count() > 1) 
       { 
        return View("List", offenders); 
       } 
       else 
       { 
        return RedirectToAction("Details", 
         new { id = offenders.First().SPN }); 
       } 
      } 
     } 


     // 
     // GET: /Home/Details/5 

     public ActionResult Details(int id) 
     { 
      return View(); 
     } 

     // 
     // GET: /Home/Create 

     public ActionResult Create() 
     { 
      return View(); 
     } 

     // 
     // POST: /Home/Create 

     [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Create(FormCollection collection) 
     { 
      try 
      { 
       // TODO: Add insert logic here 

       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 

     // 
     // GET: /Home/Edit/5 

     public ActionResult Edit(int id) 
     { 
      return View(); 
     } 

     // 
     // POST: /Home/Edit/5 

     [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Edit(int id, FormCollection collection) 
     { 
      try 
      { 
       // TODO: Add update logic here 

       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 

     } 

     public ActionResult About() 
     { 
      return View(); 
     } 

    } 
} 

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

namespace DOC_Kools 
{ 
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801 

    public class MvcApplication : System.Web.HttpApplication 
    { 
     public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

      routes.MapRoute(
       "Default",            // Route name 
       "{controller}/{action}/{id}",       // URL with parameters 
       new { controller = "Home", action = "Index", id = "" } // Parameter defaults 
      ); 

      routes.MapRoute(
       "OffenderSearch", 
       "Offenders/Search/{searchTerm}", 
       new 
       { 
        controller = "Home", 
        action = "Index", 
        searchTerm = "" 
       } 
         ); 
      routes.MapRoute(
       "OffenderAjaxSearch", 
       "Offenders/getAjaxResult/", 
       new { controller = "Home", action = "getAjaxResult" } 
       ); 


     } 

     protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 

      RegisterRoutes(RouteTable.Routes); 
     } 
    } 
} 

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<DOC_Kools.Models.Offender>" %> 

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server"> 
    <script src="../../Scripts/jquery.autocomplete.js" type="text/javascript"></script> 
    <script src="../../Scripts/jquery-1.3.2.js" type="text/javascript"></script> 

<script type="text/javascript"> 

    $(document).ready(function() { 
     $("#searchTerm").autocomplete("/Offenders/getAjaxResult/"); 
    }); 

</script> 
    Home Page 

</asp:Content> 

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server"> 
    <h2><%= Html.Encode(ViewData["Message"]) %></h2> 


     <h2>Look for an offender</h2> 

    <form action="/Offenders/Search" method="post" id="searchForm"> 
     <input type="text" name="searchTerm" id="searchTerm" value="" size="10" maxlength="30" /> 
     <input type="submit" value="Search" /> 

    </form> 
    <br /> 



</asp:Content> 

我有什么为了使搜索文本框上显示的做索引页面? 我还需要做些什么才能使自动完成功能正常工作。我有autocomplete.js & jquery.js添加到index.aspx视图

任何帮助将不胜感激,让我可以得到这个工作。

谢谢!

+0

这是错误消息详细信息 '/'应用程序中的服务器错误。 ------------------------------------------------- ------------------------------- 无法找到该资源。 描述:HTTP 404.您正在查找的资源(或其某个依赖项)可能已被删除,名称已更改或暂时不可用。请检查以下网址并确保它拼写正确。 请求的网址:/违者/搜索 – 2010-03-12 21:58:22

+0

你是否正确地在你的ajax调用中设置了你的URL?我的意思是指向你的行动... – 2010-03-15 16:05:25

+0

我不确定这是我使用MVC 2的第一个项目,而不是真正熟悉.NET。我在想我没有正确配置我的路由表,这是我在上面的代码中包含的。我有点困惑与全局页面中的路由表。 – 2010-03-15 20:25:33

回答

0

也许这是在global.asax路由的顺序? 尝试并颠倒顺序。我认为它试图从头到尾找到正确的路线,在你的情况下,它总是在第一条路线中停留:“{控制器}/{动作}/{编号}”...

+0

Ok danfromisrael,当我颠倒了第一和第三地图路线的顺序,我运行该应用程序。一旦我输入名称并单击搜索,它就会返回到没有任何信息的搜索页面,并要求我输入另一个名称进行搜索。 – 2010-04-09 14:49:27

相关问题