2015-06-02 297 views
0

我絕對難住這個,希望有人能幫助我。我跟着例如設置在這裏MVC過濾器不工作

http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

,我發誓我已經篩選工作正常,但後來我回來就發現它不再工作。當我運行頁面,我在過濾器中,然後按回車東西「搜索」,我得到以下錯誤:

Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

請求的URL:/ HomeController中/交易

我在我的HomeController以下方法的.cs。

public ActionResult Transactions(string sortOrder, string currentFilter, string searchString, int? page) 
    { 
     ViewBag.CurrentSort = sortOrder; 

     ViewBag.DateSortParm = sortOrder == "Date" ? "Date_Desc" : "Date"; 
     ViewBag.AmountSortParm = sortOrder == "Amount" ? "Amount_Desc" : "Amount"; 
     ViewBag.DescriptionSortParm = sortOrder == "Desc" ? "Desc_Desc" : "Desc"; 
     ViewBag.TableNameSortParm = sortOrder == "TableName" ? "TableName_Desc" : "TableName"; 
     ViewBag.CatTypeSortParm = sortOrder == "CatType" ? "CatType_Desc" : "CatType"; 
     ViewBag.CatNameSortParm = sortOrder == "CatName" ? "CatName_Desc" : "CatName"; 

     if (searchString != null) 
     { 
      page = 1; 
     } 
     else 
     { 
      searchString = currentFilter; 
     } 

     ViewBag.CurrentFilter = searchString; 

     var transactions = from s in db.Transactions 
         select s; 

     if (!String.IsNullOrEmpty(searchString)) 
     { 
      transactions = transactions.Where(s => s.TranDescription.ToUpper().Contains(searchString.ToUpper()) 
            || s.Account.Description.ToUpper().Contains(searchString.ToUpper()) 
            || s.Category.CategoryType.CatType.ToUpper().Contains(searchString.ToUpper()) 
            || s.Category.CatName.ToUpper().Contains(searchString.ToUpper())); 
     } 

     switch (sortOrder) 
     { 
      case "Date": 
       transactions = transactions.OrderBy(s => s.TranDate); 
       break; 
      case "Date_Desc": 
       transactions = transactions.OrderByDescending(s => s.TranDate); 
       break; 
      case "Amount": 
       transactions = transactions.OrderBy(s => s.TranAmount); 
       break; 
      case "Amount_Desc": 
       transactions = transactions.OrderByDescending(s => s.TranAmount); 
       break; 
      case "Desc": 
       transactions = transactions.OrderBy(s => s.TranDescription); 
       break; 
      case "Desc_Desc": 
       transactions = transactions.OrderByDescending(s => s.TranDescription); 
       break; 
      case "TableName": 
       transactions = transactions.OrderBy(s => s.Account.Description); 
       break; 
      case "TableName_Desc": 
       transactions = transactions.OrderByDescending(s => s.Account.Description); 
       break; 
      case "CatType": 
       transactions = transactions.OrderBy(s => s.Category.CategoryType.CatType); 
       break; 
      case "CatType_Desc": 
       transactions = transactions.OrderByDescending(s => s.Category.CategoryType.CatType); 
       break; 
      case "CatName": 
       transactions = transactions.OrderBy(s => s.Category.CatName); 
       break; 
      case "CatName_Desc": 
       transactions = transactions.OrderByDescending(s => s.Category.CatName); 
       break; 
      default: 
       transactions = transactions.OrderByDescending(s => s.Account.Description); 
       break; 
     } 

     int pageSize = 10; 
     int pageNumber = (page ?? 1); 
     return View(transactions.ToPagedList(pageNumber, pageSize)); 
    } 

而我在我的視圖(Transactions.cshtml)中有以下代碼。

@model PagedList.IPagedList<finance.Models.Transaction> 
 
@using PagedList.Mvc; 
 
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" /> 
 

 
@{ 
 
    ViewBag.Title = "Transactions"; 
 
} 
 

 
<h2>Transactions</h2> 
 

 
<p> 
 
    @Html.ActionLink("Create New", "Create") 
 
</p> 
 

 
@using (Html.BeginForm("Transactions", "HomeController", FormMethod.Get)) 
 
{ 
 
    <p> 
 
     Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) 
 
     <input type="submit" value="Search" /> 
 
    </p> 
 
} 
 

 
<table class="table"> 
 
    <tr> 
 
     <th> 
 
      @Html.ActionLink("Date", "Transactions", new { sortOrder = ViewBag.DateSortParm }) 
 
     </th> 
 
     <th> 
 
      @Html.ActionLink("Amount", "Transactions", new { sortOrder = ViewBag.AmountSortParm }) 
 
     </th> 
 
     <th> 
 
      @Html.ActionLink("Description", "Transactions", new { sortOrder = ViewBag.DescriptionSortParm }) 
 
     </th> 
 
     <th> 
 
      @Html.ActionLink("Account", "Transactions", new { sortOrder = ViewBag.TableNameSortParm }) 
 
     </th> 
 
     <th> 
 
      @Html.ActionLink("Main Category", "Transactions", new { sortOrder = ViewBag.CatTypeSortParm }) 
 
     </th> 
 
     <th> 
 
      @Html.ActionLink("Sub-Category", "Transactions", new { sortOrder = ViewBag.CatNameSortParm }) 
 
     </th> 
 
     <th></th> 
 
    </tr> 
 

 
@foreach (var item in Model) { 
 
    <tr> 
 
     <td> 
 
      @Html.DisplayFor(modelItem => item.TranDate) 
 
     </td> 
 
     <td> 
 
      @Html.DisplayFor(modelItem => item.TranAmount) 
 
     </td> 
 
     <td> 
 
      @Html.DisplayFor(modelItem => item.TranDescription) 
 
     </td> 
 
     <td> 
 
      @Html.DisplayFor(modelItem => item.Account.Description) 
 
     </td> 
 
     <td> 
 
      @Html.DisplayFor(modelItem => item.Category.CategoryType.CatType) 
 
     </td> 
 
     <td> 
 
      @Html.DisplayFor(modelItem => item.Category.CatName) 
 
     </td> 
 
     <td> 
 
      @Html.ActionLink("Add Category", "EditTransactions", new { id = item.ID }) 
 
     </td> 
 
    </tr> 
 
} 
 

 
</table> 
 
<br /> 
 
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount 
 

 
@Html.PagedListPager(Model, page => Url.Action("Transactions", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

這是從我的trace.axd調試信息是否有幫助

PATH_INFO /HomeController/Transactions 

PATH_TRANSLATED D:\business\personal\finance\database\web\finance\finance\HomeController\Transactions 

QUERY_STRING SearchString=test 

REMOTE_ADDR ::1 

REMOTE_HOST ::1 

REMOTE_PORT 65446 

REQUEST_METHOD GET 

SCRIPT_NAME /HomeController/Transactions 

SERVER_NAME localhost 

SERVER_PORT 60085 

SERVER_PORT_SECURE 0 

SERVER_PROTOCOL HTTP/1.1 

SERVER_SOFTWARE Microsoft-IIS/8.0 

URL /HomeController/Transactions 

HTTP_CONNECTION keep-alive 

HTTP_ACCEPT text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 

HTTP_ACCEPT_ENCODING gzip, deflate 

HTTP_ACCEPT_LANGUAGE en-US,en;q=0.5 

ASP.NET_SessionId=mjhmmkoroa3f054opatmm4yn 

HTTP_HOST localhost:60085 

HTTP_REFERER http://localhost:60085/Home/Transactions 

HTTP_USER_AGENT Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0 

回答

0

404錯誤沒有與搜索有關。它與路由有關。

嘗試/Home/Transactions/Transactions

即使你沒有足夠的查詢參數仍然會打的方法。

0

因爲你的行動的簽名看起來像public ActionResult Transactions(string sortOrder, string currentFilter, string searchString, int? page)只有page是可選的,你用發送表單時丟失參數:

@using (Html.BeginForm("Transactions", "HomeController", FormMethod.Get)) 
{ 
    <p> 
     Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) 
     <input type="submit" value="Search" /> 
</p> 
} 

您應該添加缺少的字段:

@using (Html.BeginForm("Transactions", "HomeController", FormMethod.Get)) 
{ 
    <p> 
     Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) 
     @Html.TextBox("SortOrder") 
     @Html.TextBox("CurrentFilter") 
     <input type="submit" value="Search" /> 
</p> 
} 
0

我看到網址說

/HomeController/Transactions 

它必須

/Home/Transactions 

所以這就是你需要如何創建表單

@using (Html.BeginForm("Transactions", "Home", FormMethod.Get)) 
{ 
    <p> 
     Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as  string) 
     <input type="submit" value="Search" /> 
    </p> 
} 

你有

Html.BeginForm("Transactions", "HomeController", FormMethod.Get) 

Html.BeginForm("Transactions", "Home", FormMethod.Get) 

MVC將解決的HomeController的您。

https://msdn.microsoft.com/en-us/library/dd492692(v=vs.118).aspx

退房的例子。

+0

乾杯隊友。這解決了它。非常感激。 – Gadz

+0

你能把這個標記爲答案嗎? – dariogriffo