我有一个简单的ASP.NET MVC应用程序,它可以搜索,过滤和分页车辆列表。我按照youtube https://www.youtube.com/watch?v=srN56uxw76s
上的 kudvenkat 的教程,在控制器上实现了排序,过滤和分页(在"索引"操作)所以,这是我的" VehicleMakeController"和"索引"动作:
public class VehicleMakeController : Controller
{
private readonly IVehicleRepository _vehicleRepository;
public VehicleMakeController()
{
_vehicleRepository = new VehicleRepository(new VehicleDbContext());
}
// GET: VehicleMake
public ActionResult Index(string search, int? page, string sort)
{
ViewBag.SortNameParameter = string.IsNullOrEmpty(sort) ? "Name desc" : "";
var makes = _vehicleRepository.AllMakes;
switch (sort)
{
case "Name desc":
makes = makes.OrderByDescending(x => x.Name);
break;
default:
makes = makes.OrderBy(x => x.Name);
break;
}
if (search == null)
{
return View(makes.ToList().ToPagedList(page ?? 1, 5));
}
return View(makes.Where(x => x.Name.ToLower().StartsWith(search)).ToList().ToPagedList(page ?? 1, 5));
}
这是我的"索引"视图:
@using PagedList;
@using PagedList.Mvc;
@model IPagedList<Project.Service.Entities.VehicleMake>
@{
ViewBag.Title = "Vehicle Makes";
}
<h2>@ViewBag.Title</h2>
@Html.ActionLink("Create", "CreateVehicleMake")
<br/>
<br/>
@using (@Html.BeginForm("Index", "VehicleMake", FormMethod.Get))
{
<p>
@Html.TextBox("search") <input type="submit" value="Search"/>
</p>
}
<table class="table">
<thead>
<tr>
<th>@Html.ActionLink("Name", "Index", new { sort = ViewBag.SortNameParameter, search = Request.QueryString["search"] })</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var vehicleMake in Model)
{
<tr>
<td>@vehicleMake.Name</td>
<td>@Html.ActionLink("Edit", "EditVehicleMake", new {id = vehicleMake.Id})</td>
<td>@Html.ActionLink("Delete", "DeleteVehicleMake", new {id = vehicleMake.Id})</td>
</tr>
}
</tbody>
</table>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, search = Request.QueryString["search"], sort = Request["sort"]}),
new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true})
@if (!Model.Any())
{
<b>No rows match search criteria!</b>
}
在应用程序中,我使用了存储库模式,并且我有#34; AllMakes&#34;从数据库中检索所有车辆的方法。所以,这是我的&#34; VehicleRepository&#34;:
public class VehicleRepository : IVehicleRepository
{
private readonly VehicleDbContext _context;
public VehicleRepository(VehicleDbContext context)
{
_context = context;
}
public IEnumerable<VehicleMake> AllMakes => _context.VehicleMakes;
}
这是我的#e; IVehicleRepository&#34;接口:
public interface IVehicleRepository
{
IEnumerable<VehicleMake> AllMakes { get; }
}
我的DbContext类如下:
public class VehicleDbContext : DbContext
{
public VehicleDbContext() : base("VehicleDbContext")
{
}
public DbSet<VehicleMake> VehicleMakes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
这一切正常,但现在我想在存储库中实现排序和分页,并将其从控制器中删除。参数应该通过get / query方法传递。我不知道该怎么做,我将不胜感激。
答案 0 :(得分:0)
您可以在IVehicleRepository
中声明一个函数并实现它。
public interface IVehicleRepository
{
IEnumerable<VehicleMake> AllMakes { get; }
List<VehicleMake> GetVehicleWithPagination(string search, int? page, string sort);
}
public class VehicleRepository : IVehicleRepository
{
// your old code
public List<VehicleMake> GetVehicleWithPagination(string search, int? page, string sort)
{
// this is your code from controller
switch (sort)
{
case "Name desc":
makes = AllMakes.OrderByDescending(x => x.Name);
break;
default:
makes = AllMakes.OrderBy(x => x.Name);
break;
}
if (search == null)
{
return AllMakes.ToList().ToPagedList(page ?? 1, 5);
}
}
}
你的控制器:
public ActionResult Index(string search, int? page, string sort)
{
ViewBag.SortNameParameter = string.IsNullOrEmpty(sort) ? "Name desc" : "";
return View(_vehicleRepository.GetVehicleWithPagination(search, page, sort));
}