我的Kendo UI Grid工作正常,但我遇到页面刷新问题。我注意到,如果我刷新浏览器,我刚插入的数据就会消失。单元仍然存在,因为Grid识别出应该存在数据并且Read action方法正在触发并返回适当的数据,但数据不会显示。有什么想法吗?
控制器:
public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request, ExpenseReportModel erLineItem, int expenseReportId)
{
if (Request.IsAjaxRequest())
{
return Json(globalKip.DisplayExpenseReportLineItems_Employee(expenseReportId).ToList().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
else
{
return View("Index", erLineItem);
}
}
网格标记:
@(Html.Kendo().Grid<ExpenseReport.MVC.Models.ExpenseReportModel>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.ExpenseReportId).Visible(true);
columns.Bound(p => p.ExpenseLineItemId).Visible(true);
columns.Bound(p => p.ExpenseTypeDesc).Title("Expense Type");
columns.Bound(p => p.City).Title("City");
columns.Bound(p => p.StateName).Title("State");
columns.Bound(p => p.Date).Format("{0:d}").Title("Date");
columns.Bound(p => p.Amount).Title("Amount");
columns.Bound(p => p.EndingMileage).Title("Ending Mileage");
columns.Bound(p => p.BeginningMileage).Title("Beginning Mileage");
columns.Command(command => { command.Edit(); command.Destroy(); });
})
.ToolBar(toolbar => toolbar.Create().HtmlAttributes(new { id = "btnAdd" }))
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("NewExpense").Window(w => w.Width(500)))
.Pageable()
.Scrollable()
.HtmlAttributes(new { style = "height:430px; width=100%" })
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
//.Events(events => events.Sync("onRequestStart"))
.Model(model => model.Id("ExpenseLineItemId"))
.Create(create => create
.Action("EditingPopup_Create", "ExpenseReport")
.Data("erLineItemsCreateData"))
.Read(read => read
.Action("EditingPopup_Read", "ExpenseReport")
.Data("erLineItemsReadData"))
.Update(update => update
.Action("EditingPopup_Update", "ExpenseReport"))
.Destroy(destroy => destroy
.Action("EditingPopup_Destroy", "ExpenseReport").Type(HttpVerbs.Post)))
)
//pass additional data to the READ action method
function erLineItemsReadData() {
return {
expenseReportId: "@ViewBag.ExpenseReportId"
};
}
function erLineItemsCreateData() {
return {
expenseReportId: "@ViewBag.ExpenseReportId"
};
}
function erLineItemsUpdateData() {
return {
expenseReportId: "@ViewBag.ExpenseReportId"
};
}
更新
我设法解决了部分问题。请参阅下面的代码。现在,当我创建一行时,在浏览器刷新后维护该行。但是,现在首次加载页面时,会在网格中插入一个新的空白行。我不想要这种行为。此外,如果我添加多行并刷新页面,除了我创建的最后一行之外的所有行都会消失。
public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request, ExpenseReportModel erLineItem, int expenseReportId)
{
if (Request.IsAjaxRequest())
{
var results = globalKip.DisplayExpenseReportLineItems_Employee(expenseReportId).ToList();
for (int i = 0; i < results.Count; i++) // Loop through List with for
{
erLineItem.ExpenseReportId = (int)results[i].ERL_EPR_ID;
erLineItem.ExpenseLineItemId = results[i].ERL_ID;
erLineItem.ExpenseTypeDesc = results[i].ERL_ExpenseType;
erLineItem.City = results[i].ERL_City;
erLineItem.StateName = results[i].ERL_State;
erLineItem.Date = (DateTime)results[i].ERL_Date;
erLineItem.Amount = (Decimal)results[i].ERL_DollarAmount;
erLineItem.EndingMileage = (int)results[i].ERL_EndingMileage;
erLineItem.BeginningMileage = (int)results[i].ERL_BeginningMileage;
}
//return Json(new[] { erLineItem }.ToDataSourceResult(request, ModelState));
return Json(new[] { erLineItem }.ToDataSourceResult(request, ModelState));
//return Json(globalKip.DisplayExpenseReportLineItems_Employee(expenseReportId).ToList().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
else
{
return View("Index", erLineItem);
}
}
答案 0 :(得分:0)
我弄明白了这个问题。从存储过程结果返回的字段名称与我的模型中的字段名称不匹配。我重命名它们并通过另一个小修改修复了整个问题(使用IQuerable而不是for循环)。