我已经使用WebAPI和KendoUI创建了一个.NET Core应用程序。我添加了一个通用存储库,当我尝试将数据源添加到Kendo Grid时,我收到以下.ToDataSourceResult()
消息的错误:
>不包含以下定义 “ ToDataSourceResult”和最佳扩展方法重载 'QueryableExtensions.ToDataSourceResult(DataTable,DataSourceRequest)' 需要类型为“ DataTable”的接收器
我遵循有关Core和WebApi的Kendo文档来完成此方法,但无法使其正常工作。我已经附加了项目中与此错误和数据相关的代码。
Controllers / 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 /船只存储库
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);
}
}
接口/ IVessel存储库
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
答案 0 :(得分:1)
对于ToDataSourceResult
,这是IEnumerable
的方法。
对于GetAllVesselsAsync()
,它返回一个任务。
尝试下面的代码
[HttpGet]
public async Task<DataSourceResult> GetVessels([DataSourceRequest]DataSourceRequest request)
{
var result = await service.GetAllVesselsAsync();
return result.ToDataSourceResult(request);
}
更新
public class HomeController : Controller
{
private IVesselRepository service;
public HomeController(IVesselRepository service)
{
this.service = service;
}
答案 1 :(得分:1)
这是我的Kendo MVC网格,需要异步功能。 在复杂的情况下,您需要使用异步DataSourceResult来绑定来自api的网格数据:
public class HomeController : Controller
{
private HttpClient _client = ApiHelper.Client;
private Uri baseUri = ApiHelper._Client.BaseAddress;
public ActionResult Index()
{
return View();
}
DataSourceResult result;
JsonResult final;
public async Task<ActionResult> GetList([DataSourceRequest] DataSourceRequest request)
{
int offset = 0;
const int limit = 100;
List<myModel> myModelList = new List<myModel>();
using (var req = new HttpRequestMessage(HttpMethod.Get, baseUri + ConfigurationManager.AppSettings["V3BaseUri"] + "resource?offset=" + offset + "&limit=" + limit))
using (var response = await ApiHelper.Client.SendAsync(req))
{
response.EnsureSuccessStatusCode();
try
{
var content = response.Content.ReadAsStringAsync().Result;
myModelList = JsonConvert.DeserializeObject<List<myModel>>(content);
result = await myModelList.ToDataSourceResultAsync(request);
final = Json(result);
return final;
}
catch (Exception exc)
{
string message = exc.Message;
Console.WriteLine("Exception message: {0}", message);
throw;
}
}
}
在我看来:
@(Html.Kendo().Grid<Project.Models.myModel>()
.Name("grid")
.AutoBind(true)
.Columns(columns =>
{
columns.Bound(c => c.Id)
.Width(80).Title("Entity Id");
columns.Bound(c => c.NameOfInstitution);
columns.Bound(c => c.Addresses).ClientTemplate("#=generateAddress(Addresses)#").Title("Address");
columns.Bound(c => c.InstitutionTelephones).ClientTemplate("#=generatePhone(InstitutionTelephones)#").Title("Phone");
})
.HtmlAttributes(new { style = "height: 750px;" })
.Scrollable()
.Groupable()
.Sortable()
.Pageable()
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(99)
.Read(read => read.Action("GetList", "Home"))
).NoRecords(x => x.Template("<div class='empty-grid'>NO RECORDS</div>")))