我尝试在网格中测试 ajax 绑定数据。如果数据列是数字(int、double、float 等),我们会收到错误或字典和模型数据类型不兼容:传入字典的模型项的类型为“System.Int32”,但该字典需要模型项'System.String' 类型。
这发生在版本 1.319 中。在 1.315 的早期版本中,它运行良好。
控制器:
public ActionResult Ajax()
{
return View();
}
public ActionResult AjaxRead([DataSourceRequest] DataSourceRequest request)
{
var persons = new List<Person>()
{
new Person() {Id = 1, Name = "Jack", Age = 13},
new Person() {Id = 2, Name = "Riley", Age = 45},
new Person() {Id = 3, Name = "William", Age = 34},
new Person() {Id = 4, Name = "Oliver", Age = 66},
new Person() {Id = 5, Name = "Lachlan", Age = 35},
new Person() {Id = 6, Name = "Thomas", Age = 53},
new Person() {Id = 7, Name = "Lachlan", Age = 24},
new Person() {Id = 8, Name = "John", Age = 54},
new Person() {Id = 9, Name = "Alex", Age = 19},
new Person() {Id = 10, Name = "Victor", Age = 39},
new Person() {Id = 11, Name = "Joshua", Age = 33}
};
return Json(persons.ToDataSourceResult(request));
}
}
public class Person
{
[ScaffoldColumn(false)]
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
看法:
@(Html.Kendo().Grid<Kendo.Mvc.Examples.Controllers.Person>()
.Name("Grid")
.Columns(columns => {
columns.Bound(p => p.Id).Groupable(false);
columns.Bound(p => p.Name);
columns.Bound(p => p.Age);
})
.Pageable()
.Sortable()
.Filterable()
.Groupable()
.Editable(editable => editable.Mode(GridEditMode.PopUp))
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(8)
.Model(model => model.Id(p => p.Id))
.Read("AjaxRead", "Grid")
)
)
如果我将代码更改如下
public class Person
{
[ScaffoldColumn(false)]
public int Id { get; set; }
public string Name { get; set; }
[DataType("Integer")]
public int Age { get; set; }
}
不会发生错误。
我不想为我的应用程序的所有类的所有数字字段设置属性,尤其是在以前的版本中它运行良好。
但如果没有,请告诉我我做错了什么。
谢谢你,弗拉德