Grid 不会 post - 批量编辑-Telerik UI for ASP.NET Core
Grid will not post - Batch Editing-Telerik UI for ASP.NET Core
我使用 ASP.Net Core MVC 3.1 和 EF 3.1 创建了一个网络应用程序。
我添加了一个网页,使用 ASP.Net Core Telerik Grid 分批(按年级)更新学生电话号码。页面顶部有一个小表格,有一些按钮,这些按钮允许 select 您想要更新学生电话号码的年级。
ASP.Net Core Telerik 网格在点击评分按钮后显示相关学生的数据。
所以我的问题是,当我在进行必要的编辑后使用网格的保存更改按钮时,它会触发我的更新操作方法。但是网格没有向操作方法传递任何内容,因此它接收到 null。我可以知道我在代码中犯了什么错误吗?
我的编码如下
Index.cshtml
@model School_MGT.Models.Students
<div class="container">
<div class="container">
<form asp-controller="AttendenceStudents" asp-action="Index">
<p align="center">
<input type="submit" name="btn" value="1A" />
<input type="submit" value="1B" name="btn" />
</p>
</form>
</div>
</div>
@(Html.Kendo().Grid<School_MGT.Models.Students>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.Add_No);
columns.Bound(p => p.S_Name).Width(140);
columns.Bound(p => p.Tel_H).Width(140);
})
.ToolBar(toolbar =>
{
toolbar.Create();
toolbar.Save();
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Navigatable()
.Sortable()
.Scrollable()
.Resizable(resize => resize.Columns(true))
.DataSource(dataSource => dataSource
.Ajax()
.Batch(true)
.PageSize(20)
.ServerOperation(false)
.Model(model => model.Id(p => p.Add_No))
.Update("Update", "AttendenceStudents")
)
)
AttendenceStudentsController
namespace School_MGT.Controllers
{
public class AttendenceStudentsController : Controller
{
private readonly ConnectionString _context;
public AttendenceStudentsController(ConnectionString context)
{
_context = context;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(string btn)
{
var Students = from m in _context.Students select m;
switch (btn)
{
case "1A":
Students = Students.Where(x => x.Grade == "1" && x.Class == "A");
return View();
case "1B":
Students = Students.Where(x => x.Grade == "1" && x.Class == "B");
return View();
default:
return (View());
}
}
[HttpPost]
public IActionResult Update([Bind("RowID,Add_No,S_Name")] Students target)
{
Students entity = _context.Students.FirstOrDefault((r => r.Add_No == target.Add_No));
entity.Tel_H = target.Tel_H;
_context.SaveChanges();
return View();
}
}
}
当您在 DataSouce 定义的 .Update()
方法中定义更新操作方法时,您仅将操作方法和控制器名称作为参数传递,而忽略了 lambda 片段。
你有:
.Update("Update", "AttendenceStudents")
尝试以下操作:
Update(update => update.Action("Update", "AttendenceStudents"))
这遵循了 Telerik 在其 Grid 中针对 ASP.NET 核心批量编辑 walkthrough example 提出的建议。请参阅第 6 步底部的代码。
我使用 ASP.Net Core MVC 3.1 和 EF 3.1 创建了一个网络应用程序。 我添加了一个网页,使用 ASP.Net Core Telerik Grid 分批(按年级)更新学生电话号码。页面顶部有一个小表格,有一些按钮,这些按钮允许 select 您想要更新学生电话号码的年级。 ASP.Net Core Telerik 网格在点击评分按钮后显示相关学生的数据。
所以我的问题是,当我在进行必要的编辑后使用网格的保存更改按钮时,它会触发我的更新操作方法。但是网格没有向操作方法传递任何内容,因此它接收到 null。我可以知道我在代码中犯了什么错误吗?
我的编码如下
Index.cshtml
@model School_MGT.Models.Students
<div class="container">
<div class="container">
<form asp-controller="AttendenceStudents" asp-action="Index">
<p align="center">
<input type="submit" name="btn" value="1A" />
<input type="submit" value="1B" name="btn" />
</p>
</form>
</div>
</div>
@(Html.Kendo().Grid<School_MGT.Models.Students>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.Add_No);
columns.Bound(p => p.S_Name).Width(140);
columns.Bound(p => p.Tel_H).Width(140);
})
.ToolBar(toolbar =>
{
toolbar.Create();
toolbar.Save();
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Navigatable()
.Sortable()
.Scrollable()
.Resizable(resize => resize.Columns(true))
.DataSource(dataSource => dataSource
.Ajax()
.Batch(true)
.PageSize(20)
.ServerOperation(false)
.Model(model => model.Id(p => p.Add_No))
.Update("Update", "AttendenceStudents")
)
)
AttendenceStudentsController
namespace School_MGT.Controllers
{
public class AttendenceStudentsController : Controller
{
private readonly ConnectionString _context;
public AttendenceStudentsController(ConnectionString context)
{
_context = context;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(string btn)
{
var Students = from m in _context.Students select m;
switch (btn)
{
case "1A":
Students = Students.Where(x => x.Grade == "1" && x.Class == "A");
return View();
case "1B":
Students = Students.Where(x => x.Grade == "1" && x.Class == "B");
return View();
default:
return (View());
}
}
[HttpPost]
public IActionResult Update([Bind("RowID,Add_No,S_Name")] Students target)
{
Students entity = _context.Students.FirstOrDefault((r => r.Add_No == target.Add_No));
entity.Tel_H = target.Tel_H;
_context.SaveChanges();
return View();
}
}
}
当您在 DataSouce 定义的 .Update()
方法中定义更新操作方法时,您仅将操作方法和控制器名称作为参数传递,而忽略了 lambda 片段。
你有:
.Update("Update", "AttendenceStudents")
尝试以下操作:
Update(update => update.Action("Update", "AttendenceStudents"))
这遵循了 Telerik 在其 Grid 中针对 ASP.NET 核心批量编辑 walkthrough example 提出的建议。请参阅第 6 步底部的代码。