我正在根据书的出版日期和关键词搜索参数对书籍索引进行排序。我正在使用一个switch语句,该语句选择枚举值,这些枚举值将以不同的顺序(按升序还是降序)告诉操作指令。但是,似乎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>
答案 0 :(得分:1)
首先,枚举始终具有相同值的最常见原因是未正确分配值,因此将其分配为其默认值。枚举的默认值为0
,这是您在Classification
枚举中定义的第一个值。在您的情况下,NewestFirst
似乎是默认值。此行为表示未传递DateValueSign
值。
第二,对于调试可能与Razor渲染有关的问题,请始终检查检查元素以验证生成的代码。第三,在处理诸如case语句之类的服务器端问题时,debugger非常方便。
关于您的问题,MVC需要枚举的 number 值,否则默认为0。如果您在检查元素中选中单选按钮,则会注意到value
属性应该显示value="LeastPopular"
之类的字符串时显示value="3"
之类的字符串。发生这种情况是因为RadioButton
函数将任何枚举指定为字符串。为此,只需将剃刀中的每个枚举都转换为int
。
例如,此:
<label class="radio">@Html.RadioButton("DateValueSign", Classification.NewestFirst)Newest First</label>
成为这个:
<label class="radio">@Html.RadioButton("DateValueSign", (int) Classification.NewestFirst)Newest First</label>
答案 1 :(得分:0)
对于您的问题,这是由于您将<label class="radio">@Html.RadioButton("DateValueSign", Classification.NewestFirst)Newest First</label>
放置在<form asp-action="Index" asp-controller="Home" method="get">
之外,然后无论您为DateValueSign
选择什么,它都不会传递给控制器和使用Classification
的默认值。
将@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>