2015-06-02 300 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