MVC过滤器无法正常工作

时间:2015-06-02 13:30:45

标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-4

我对这一点感到非常难过,并希望有人能帮助我。我按照这里列出的例子进行了

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

我发誓我过滤工作正常然后我回到它发现它不再有效。当我运行页面并在过滤器中输入内容并按“搜索”时,出现以下错误:

  '/'应用程序中的服务器错误。   无法找到该资源。   说明:HTTP 404.您要查找的资源(或其中一个依赖项)可能已被删除,名称已更改或暂时不可用。请查看以下网址,确保拼写正确。

请求的网址:/ HomeController / Transactions

我的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

3 个答案:

答案 0 :(得分:0)

404错误与搜索无关。它与路由有关。

尝试/Home/Transactions/Transactions

即使您没有查询参数,它仍然会触及该方法。

答案 1 :(得分: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>
}

答案 2 :(得分: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

查看示例。