我需要使用开源KendoUI Grid从模态显示我的列表。但它并不成功。我连接数据库并将数据作为控制器类中的列表。我需要从数据库中获取数据来执行此网格。
$("#gridd").kendoGrid({
dataSource: {
transport: {
read: {
url: "report/GetData",
type:"json"
}
},
sortable: true,
pageable: {
input: true,
numeric: false
}, height: 430,
selectable: "multiple",
columns: [
{ field: "Users.uName", title: "Kullanıcı", width: "80px" },
{ field: "Locations.locName", title: "Oda", width: "80px" },
{ field: "Devices.devName", title: "Cihaz", width: "80px" },
{ field: "Commands.cName", title: "Komut", width: "80px" },
{ field: "gasValue", title: "Gaz", width: "80px" },
{ field: "tempValue", title: "Sıcaklık", width: "130px" },
{ field: "humValue", title: "Nem", width: "80px" },
{ field: "AlarmCodes.aName", title: "Alarm", width: "80px" },
{ field: "ReasonCodes.rName", title: "Nedeni", width: "80px" }]
}
});
我的控制器类
public JsonResult GetData()
{
var reports = db.ActivityLog.OrderBy(c => c.dateTime).ToList();
return Json(reports, JsonRequestBehavior.AllowGet);
}
我编辑我当前的代码。现在我看到网格,但我看不到里面的数据。他们将如何展示?
答案 0 :(得分:1)
<div id="grid" ></div>
<div id="details"></div>
<script>
var wnd, detailsTemplate;
$(document).ready(function () {
$("#grid").kendoGrid({
sortable: true,
pageable: {
input: true,
numeric: false
},
height: 430,
selectable: "multiple",
dataSource: {
transport: {
read: "/Index/Getdata",
type: "json"
}
},
columns: [
{ field: "username", title: "User", width: "80px" },
{ field: "location", title: "Location", width: "80px" },
{ field: "gas", title: "Gas Value", width: "80px" },
{ field: "temp", title: "Temp Value", width: "130px" },
{ field: "hum", title: "Hum Value", width: "80px" }]
});
});
我的控制器,在这里响应数据必须序列化。谢谢大家。
public JsonResult Getdata()
{
var reports = db.ActivityLog.OrderBy(c => c.dateTime).ToList();
var collection = reports.Select(x => new
{
username = x.Users.uName,
location = x.Locations.locName,
gas = x.gasValue,
temp = x.tempValue,
hum = x.humValue
});
return Json(collection, JsonRequestBehavior.AllowGet);
}
答案 1 :(得分:0)
您必须在视图的控制器中提供Action,并在网格的read方法中将其作为JSon对象返回。以下代码通过使用Razor引擎显示您和示例:
@(Html.Kendo().Grid<System.Data.DataRow>()
.Name("grdLocations")
.Columns(columns =>
{
columns.Bound("LocationId").Visible(false);
columns.Bound("Name").Title("Nombre").ClientTemplate("<strong>#:Name # </strong>");
columns.Bound("Latitude").Title("Latitud").Format("{0:n6}");
columns.Bound("Longitude").Title("Longitud").Format("{0:n6}");
columns.Bound("Altitude").Title("Altitud");
columns.Bound("Comments").Title("Comentario");
columns.Command(cmd => { }).Width(90);
})
.Pageable()
.Sortable()
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.ServerOperation(false)
.Model(model =>
{
model.Id("LocationId");
model.Field("LocationId", typeof(int));
model.Field("Name", typeof(string));
model.Field("Latitude", typeof(decimal));
model.Field("Longitude", typeof(decimal));
model.Field("Altitude", typeof(decimal));
model.Field("Comments", typeof(string));
})
.Read(read => read.Action("Read", "Location"))
))
如你所见,我们有“.Read()”来设置控制器的动作。
public virtual ActionResult Read([DataSourceRequest] DataSourceRequest request)
{
try
{
return Json(Location.GetList().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
EventLog.Handle(ex);
throw;
}
}
“Location”类有一个静态方法,它返回从数据库中填充的DataTable。 希望它有所帮助!