I created a web app using ASP.Net Core MVC 3.1 With EF 3.1. I added a web page to update student Telephone numbers as batches (Grade wise) using ASP.Net Core Telerik Grid. There is a small form at the top of the Page and there are buttons and those allow to select the Grade that you want to update Telephone numbers of students. And the ASP.Net Core Telerik Grid shows relevant student's data after clicking on a Grade button.
So My question is when I use the save changes button of the Grid after making required editing it triggers my Update action method. But the grid passes nothing to the action method so it receives null. May I know what can be the mistake I made in my code?
My coding as follows
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();
}
}
}
When you define your Update Action method in the .Update()
method of your DataSouce definition, you pass only the action method and controller names as arguments and leave out the lambda piece.
You have:
.Update("Update", "AttendenceStudents")
Try the following:
Update(update => update.Action("Update", "AttendenceStudents"))
This follows the advice Telerik advises in their Grid for ASP.NET Core Batch Editing walkthrough example. See the code at the bottom of Step 6.