【问题标题】:MVC Application, Orderby not working in Home Controller IndexMVC 应用程序,Orderby 在主控制器索引中不起作用
【发布时间】:2018-12-03 15:06:49
【问题描述】:

我正在根据图书的出版日期和关键字搜索参数对图书索引进行排序。我正在使用一个 switch 语句来选择枚举值,这些值会以不同的方式告诉 Action to Order 搜索结果,升序或降序。但是,Orderby 似乎不起作用,但第一个 case 语句确实起作用。无论如何,该程序只会按最新排序。最旧的第一个不起作用。

public IActionResult Index(String SearchString, Classification DateValueSign)
    {


        var query = from r in _db.Books select r;

        if (SearchString != null && SearchString != "")
        {
            query = query.Where(x => x.Title.Contains(SearchString) || x.Author.Contains(SearchString) || x.Genre.GenreName.Contains(SearchString));

        }
        switch (DateValueSign)
        {
            case Classification.NewestFirst:
                query = query.OrderByDescending(x => x.PublicationDate);
                break;
            case Classification.OldestFirst:
                query = query.OrderBy(x => x.PublicationDate);
                break;
            case Classification.MostPopular:
                query = query.OrderByDescending(x => x.AverageRating);
                break;
            case Classification.LeastPopular:
                query = query.OrderBy(x => x.AverageRating);
                break;
        }
        List<Book> SelectedBooks = new List<Book>();
        SelectedBooks = query.ToList();
        ViewBag.SelectedBooks = SelectedBooks.Count();
        ViewBag.TotalBooks = _db.Books.Count();

        return View(SelectedBooks);
    } 

这是索引的视图。快速搜索在索引页面上。

@model IEnumerable<fa18team16BevoBookStore.Models.Book>
@{
    ViewData["Title"] = "View";
}
@using fa18team16BevoBookStore.Controllers
<!--This is the quick search box code-->
<form asp-action="Index" asp-controller="Home" method="get">
    <p class="form-group">
        Search: <input name="SearchString" class="form-control" /><br />
        <button type="submit" class="btn btn-secondary">Search</button>
        <a asp-action="Index" class="btn btn-danger">Show All</a>
    </p>
</form>
<p>Displaying @ViewBag.SelectedBooks out of @ViewBag.TotalBooks </p>
<h2>View</h2>
<div class="form-group">
    <label class="radio">@Html.RadioButton("DateValueSign", 
Classification.NewestFirst)Newest First</label>
    <label class="radio">@Html.RadioButton("DateValueSign", 
Classification.OldestFirst)Oldest First</label>
    <label class="radio">@Html.RadioButton("DateValueSign", 
Classification.MostPopular)Most Popular</label>
    <label class="radio">@Html.RadioButton("DateValueSign", 
Classification.LeastPopular)Least Popular</label>
</div>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.UniqueID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Title)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Author)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Description)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.BookQuantity)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.UniqueID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Title)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Author)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Description)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.BookQuantity)
                </td>
                <td>
                    <a asp-action="Details" asp-route- 
id="@item.BookID">Details</a>
                </td>
            </tr>
        }
    </tbody>
</table>

【问题讨论】:

  • 添加断点和/或记录以查看进入控制器时的值。可能是枚举未分配但默认。

标签: c# asp.net-core-mvc


【解决方案1】:

首先,枚举始终具有相同值的最常见原因是未正确分配值,因此将其分配为其默认值。枚举的默认值为0,这是您在Classification 枚举中定义的第一个值。在您的情况下,NewestFirst 似乎是默认值。此行为意味着 DateValueSign 值未通过。

其次,对于可能与 Razor 渲染相关的调试问题,请始终检查检查元素以验证生成的代码。第三,在处理诸如 case 语句之类的服务器端问题时,debugger 非常方便。

关于您的问题,MVC 需要枚举的 number 值,否则默认为 0。如果您检查检查元素中的单选按钮,您会注意到 value 属性显示像 value="LeastPopular" 这样的字符串,而它应该显示像 value="3" 这样的东西。发生这种情况是因为RadioButton 函数将任何分配为字符串的枚举。要实现这一点,只需将每个枚举在 razor 中转换为int

例如,这个:

 <label class="radio">@Html.RadioButton("DateValueSign", Classification.NewestFirst)Newest First</label>

变成这样:

<label class="radio">@Html.RadioButton("DateValueSign", (int) Classification.NewestFirst)Newest First</label>

【讨论】:

    【解决方案2】:

    对于您的问题,是因为您将&lt;label class="radio"&gt;@Html.RadioButton("DateValueSign", Classification.NewestFirst)Newest First&lt;/label&gt; 放在&lt;form asp-action="Index" asp-controller="Home" method="get"&gt; 之外,然后无论您为DateValueSign 选择什么,它都不会传递给控制器​​并使用@ 的默认值987654324@。

    @Html.RadioButton 移至form 喜欢

    <form asp-action="Index" asp-controller="Model" method="get">
        <p class="form-group">
            Search: <input name="SearchString" class="form-control" /><br />
            <div class="form-group">
                <label class="radio">
                    @Html.RadioButton("DateValueSign", Classification.NewestFirst)Newest First
                </label>
                <label class="radio">
                    @Html.RadioButton("DateValueSign", Classification.OldestFirst)Oldest First
                </label>
                <label class="radio">
                    @Html.RadioButton("DateValueSign", Classification.MostPopular)Most Popular
                </label>
                <label class="radio">
                    @Html.RadioButton("DateValueSign", Classification.LeastPopular)Least Popular
                </label>
            </div>
    
            <button type="submit" class="btn btn-secondary">Search</button>
            <a asp-action="Index" class="btn btn-danger">Show All</a>
        </p>
    </form>
    

    【讨论】:

      猜你喜欢
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      相关资源
      最近更新 更多