我能够使用 js 版本dojo 自动编译获得工作版本,但我需要它才能使用 MVC 版本工作。我DataSourceRequest
按照下面的建议添加到控制器中,并更改了更多项目以消除我遇到的 js 错误:
//Fixed, added the schema definition
Uncaught TypeError: e.slice is not a function
当我通过控制器跟踪它时,它似乎可以工作,它返回预期的 json(如下),但由于微调器挂起并且不显示结果,它没有完成接线。
{
"Data": [{
"EmployeeId": 2147483649,
"EmployeeName": "Emily F Johnston",
"Rating": 75.0,
"LastAudited": null
}, {
"EmployeeId": 2147483687,
"EmployeeName": "Joshua Smith",
"Rating": 80.2,
"LastAudited": null
}, {
"EmployeeId": 2147483656,
"EmployeeName": "Thomas F Dunn",
"Rating": 45.0,
"LastAudited": "\/Date(1463893200000)\/"
}, {
"EmployeeId": 2147483660,
"EmployeeName": "Marjon Christine Marik",
"Rating": 88.0,
"LastAudited": null
}],
"Total": 4,
"AggregateResults": null,
"Errors": null
}
控制器:
[HttpPost]
public ActionResult EmployeeLookup(string text, [DataSourceRequest] DataSourceRequest request)
{
var filter = request?.Filters.FirstOrDefault() as FilterDescriptor;
var search = string.Empty;
if (filter != null)
{
search = filter.Value?.ToString() ?? string.Empty;
}
var employees = new List<EmployeeLookupResultEntryViewModel>();
var results = _employeeService.EmployeeLookup(search);
if (results == null)
return Json(employees.ToDataSourceResult(request));
return Json(results.ToDataSourceResult(request));
}
自动完成定义:
另外,我发现这个来自 Telerik 的文档看起来与我的用例Telerik 自定义模板非常相似,但它没有显示控制器方法,所以我无法验证它们是如何连接的。
@(Html.Kendo().AutoComplete()
.Name("Employees")
.DataTextField("EmployeeName")
.Placeholder("Search Employee")
.Filter("contains")
.IgnoreCase(true)
.MinLength(3)
.Delay(300)
.HighlightFirst(true)
.HtmlAttributes(new { style = "width:100%" })
.NoDataTemplate("Employee Not Found")
.DataSource(dataSource =>
{
dataSource.Custom()
.ServerFiltering(true)
.Type("aspnetmvc-ajax")
.Transport(transport =>
{
transport.Read("EmployeeLookup", "Employee", new {area = "Client"});
})
.Schema(schema => {schema.Data("Data");});
})
.HeaderTemplate("<div style=\"width: 400px;\" class=\"dropdown-header k-widget k-header\">" +
"<span>Id</span>" +
"<span>Name</span>" +
"<span>Pwc Rating" +
"<span>Last Audited</span>" +
"</div>")
.Template("<span style=\"width: 50px;\">#: data.EmployeeId #</span><span class=\"cell\">#: data.EmployeeName #</span><span class=\"cell\">#: data.PwcRating #</span><span class=\"cell\">#: data.LastAudited #</span>")
)
我似乎在 html 中缺少一些配置设置,因为正在返回 json/datasouce,类似于文档所述......但是小部件无法连接它。