我有一个MVC,我可以在数据库中搜索不同的值。我也可以按照升序和降序排列列表,例如奖品和字母。 但是让我说我在数据库中总共有10行。然后我进行搜索,得出两个结果。如果我然后尝试对这两个进行排序,它会再次重新加载所有10行。
如果没有重新加载整个列表,我怎样才能在搜索后进行排序?
我的控制器:
public ActionResult Index(string searchBy, string search, string sortOrder)
{
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.TextSortParm = String.IsNullOrEmpty(sortOrder) ? "text_desc" : "";
ViewBag.PriceSortParm = sortOrder == "Price" ? "price_desc" : "Price";
ViewBag.CubicMeterSortParm = sortOrder == "CubicMeter" ? "cubicMeter_desc" : "CubicMeter";
ViewBag.PricePerCubicSortParm = sortOrder == "PricePerCubic" ? "pricePerCubic_desc" : "PricePerCubic";
var text = from s in db.LearningNumbers select s;
switch (sortOrder)
{
case "name_desc":
text = text.OrderByDescending(s => s.Name);
break;
case "text_desc":
text = text.OrderByDescending(s => s.Note);
break;
case "Price":
text = text.OrderBy(s => s.Price);
break;
case "price_desc":
text = text.OrderByDescending(s => s.Price);
break;
case "CubicMeter":
text = text.OrderBy(s => s.CubicMeter);
break;
case "cubicMeter_desc":
text = text.OrderByDescending(s => s.CubicMeter);
break;
case "PricePerCubic":
text = text.OrderBy(s => s.PricePerCubic);
break;
case "pricePerCubic_desc":
text = text.OrderByDescending(s => s.PricePerCubic);
break;
}
if (!String.IsNullOrEmpty(search))
{
if (searchBy == "Tekst")
{
text = text.Where(x => x.Note.Contains(search) || search == null);
}
else
{
text = text.Where(x => x.Name.Contains(search) || search == null);
}
}
return View(text.ToList());
}
我的观点:
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
@Html.RadioButton("searchBy", "Navn", true) <text>Navn</text>
@Html.RadioButton("searchBy", "Tekst") <text>Tekst</text><br />
@Html.TextBox("search")<input class="btn btn-primary" style="margin:0 10px;" type="submit" value="Søg" />
}
<div class="col-md-12 table" style="display:table; margin:45px 0 25px 0;">
<div class="col-md-2">
@*<b>Tekst</b><br />*@
<h4>@Html.ActionLink("Navn", "Index", new { sortOrder = ViewBag.NameSortParm })</h4>
</div>
<div class="col-md-2">
@*<b>Tekst</b><br />*@
<h4>
@Html.ActionLink("Tekst", "Index", new { sortOrder = ViewBag.TextSortParm })
</h4>
</div>
<div class="col-md-2">
@*<b>Kostpris med tillæg</b><br />*@
<h4>
@Html.ActionLink("Kostpris med tillæg", "Index", new { sortOrder = ViewBag.PriceSortParm })
</h4>
</div>
<div class="col-md-2">
@*<b>Brutto m2</b><br />*@
<h4>
@Html.ActionLink("Brutto m2", "Index", new { sortOrder = ViewBag.CubicMeterSortParm })
</h4>
</div>
<div class="col-md-2">
@*<b>Kostpris pr m2 bygning</b><br />*@
<h4>
@Html.ActionLink("Kostpris pr m2 bygning", "Index", new { sortOrder = ViewBag.PricePerCubicSortParm })
</h4>
</div>
</div>
@if (Model.Count() == 0)
{
<h3>Beklager! Vi fandt desværre ingen resultater med dine søgekriterier.</h3>
<p>Prøv venligst igen.</p>
}
else
{
foreach (var item in Model)
{
<div class="col-md-12 table" style="display: table; margin: 0 0 0 0; ">
<div class="col-md-2">
<p><b>@Html.DisplayFor(modelItem => item.Name)</b></p>
</div>
<div class="col-md-2">
<p>@Html.DisplayFor(modelItem => item.Note)</p>
</div>
<div class="col-md-2">
<p>@Html.DisplayFor(modelItem => item.Price)</p>
</div>
<div class="col-md-2">
<p>@Html.DisplayFor(modelItem => item.CubicMeter)</p>
</div>
<div class="col-md-2">
<p>@Html.DisplayFor(modelItem => item.PricePerCubic)</p>
</div>
</div>
<hr />
}
}
答案 0 :(得分:2)
以上评论有它。
在功能结束之前不要返回视图。创建集合并将其保存到本地变量。
然后在该局部变量上调用您的order by子句。
的伪代码:
var results = search();
If (orderBy)
{
Results = results.orderBy(X);
}
return view(Results);