【发布时间】:2019-05-11 14:56:21
【问题描述】:
我使用 WebAPI 和 KendoUI 创建了一个 .NET Core 应用程序。我添加了一个通用存储库,当我尝试将数据源添加到我的 Kendo Grid 时,.ToDataSourceResult() 消息出现错误:
> 不包含对 'ToDataSourceResult' 和最佳扩展方法重载 'QueryableExtensions.ToDataSourceResult(DataTable,DataSourceRequest)' 需要“DataTable”类型的接收器
我按照 Core 和 WebApi 的 Kendo 文档完成了这个方法,但我无法让它工作。我已经附上了我的项目中与此错误和数据相关的代码。
控制器/HomeController
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Mvc;
using Senua.Interfaces;
using Senua.Models;
using System.Diagnostics;
namespace Senua.Controllers
{
public class HomeController : Controller
{
private IVesselRepository service;
public IActionResult Index()
{
return View();
}
[HttpGet]
public DataSourceResult GetVessels([DataSourceRequest]DataSourceRequest request)
{
return service.GetAllVesselsAsync().ToDataSourceResult(request); <----Error appears here.
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
观看次数/首页/索引
@(Html.Kendo().Grid<Senua.Models.Vessel>()
.Name("vessel_grid")
.Columns(columns =>
{
columns.Bound(p => p.Name);
})
.Pageable()
.Filterable()
.DataSource(dataSource =>
dataSource
.WebApi()
.Events(events => events.Error("error_handler"))
.Model(model =>
{
model.Id(p => p.Id);
})
.Read(read => read.Action("GetVessels", "Home"))
))
<script>
function error_handler(e) {
var errors = $.parseJSON(e.xhr.responseText);
if (errors) {
alert("Errors:\n" + errors.join("\n"));
}
}
</script>
DAL/VesselRepository
using Senua.Interfaces;
using Senua.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Senua.DAL
{
public class VesselRepository : Repository<Vessel>, IVesselRepository
{
public VesselRepository(LocalContext repositoryContext)
: base(repositoryContext)
{
}
public async Task<IEnumerable<Vessel>> GetAllVesselsAsync()
{
var vessel = await FindAllAsync();
return vessel.OrderBy(x => x.Name);
}
}
接口/IVesselRepository
using Senua.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Senua.Interfaces
{
public interface IVesselRepository
{
Task<IEnumerable<Vessel>> GetAllVesselsAsync();
Task<Vessel> GetVesselByIdAsync(int Id);
Task CreateVesselAsync(Vessel vessel);
Task UpdateVesselAsync(Vessel vessel);
Task DeleteVesselAsync(Vessel vessel);
}
}
DAL/存储库
using Senua.Interfaces;
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace Senua.DAL
{
public abstract class Repository<T> : IRepository<T> where T : class
{
protected LocalContext DbContext { get; set; }
public Repository(LocalContext _context)
{
this.DbContext = _context;
}
public async Task<IEnumerable<T>> FindAllAsync()
{
return await this.DbContext.Set<T>().ToListAsync();
}
}
}
接口/IRepository
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace Senua.Interfaces
{
public interface IRepository<T>
{
Task<IEnumerable<T>> FindAllAsync();
Task<IEnumerable<T>> FindByConditionAsync(Expression<Func<T, bool>> expression);
void Create(T entity);
void Update(T entity);
void Delete(T entity);
Task SaveAsync();
}
}
我需要一些关于这方面的指导,我以前从未设置过异步存储库,所以它有点新但是,据我所见,没关系,我已经用邮递员进行了测试。我注意到还有一个“ToDataSourceAsync”,但是在尝试之后它也没有工作。
TIA
【问题讨论】:
标签: asynchronous asp.net-core kendo-ui repository-pattern