使用 Ajax 编辑 MVC 3 Razor Telerik 网格
我正在尝试对网格中的记录进行内联编辑。
单击“保存”按钮后,关联的控制器就开始正常执行。
但是,我不知道如何将数据从模型(保存网格数据)获取到控制器操作中。有人可以帮助我吗?
这是视图的代码:
@model Telerik.Web.Mvc.GridModel<YeagerTech.YeagerTechWcfService.Customer>
@{
ViewBag.Title = "Customer Index";
}
<h2>
Customer Index</h2>
@( Html.Telerik().Grid<YeagerTech.YeagerTechWcfService.Customer>(Model.Data)
.Name("Customers")
.DataKeys(dataKeys => dataKeys.Add(o => o.CustomerID)
.RouteKey("CustomerID"))
.ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Text).ImageHtmlAttributes(new { style = "margin-left:0" }))
.Columns(columns =>
{
columns.Bound(o => o.CustomerID).Hidden(true);
columns.Command(commands =>
{
commands.Edit().ButtonType(GridButtonType.Text);
}).Width(200).Title("Command");
columns.Bound(o => o.Email).Width(200);
columns.Bound(o => o.Company).Width(200);
columns.Bound(o => o.FirstName).Width(100).Title("FName");
columns.Bound(o => o.LastName).Width(100).Title("LName");
columns.Bound(o => o.Address1).Width(200).Title("Addr1");
columns.Bound(o => o.Address2).Width(100).Title("Addr2");
columns.Bound(o => o.City).Width(100);
columns.Bound(o => o.State).Width(40).Title("ST");
columns.Bound(o => o.Zip).Width(60);
//columns.Bound(o => o.HomePhone).Width(120);
//columns.Bound(o => o.CellPhone).Width(120);
//columns.Bound(o => o.Website).Width(100);
//columns.Bound(o => o.IMAddress).Width(100);
//columns.Bound(o => o.CreatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120);
//columns.Bound(o => o.UpdatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120);
}).DataBinding(dataBinding =>
dataBinding.Ajax()
.Insert("_InsertAjaxEditing", "Customer")
.Update("_SaveAjaxEditing", "Customer"))
.Editable(editing => editing.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Scrollable()
)
这是控制器的代码:
[HttpPost]
[GridAction]
public ActionResult _SaveAjaxEditing()
{
YeagerTechWcfService.Customer cust = new YeagerTechWcfService.Customer();
if (TryUpdateModel(cust))
{
try
{
db.EditCustomer(cust); // This is a WCF method which works fine...
TempData["ErrCode"] = "Customer successfully updated.";
return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
TempData["ErrCode"] = "CustErr";
ViewBag.Error = ex.Message;
return View();
}
}
else
return View();
}
I'm trying to do an inline edit with a record in my grid.
Once I click on the Save button, my associated controller starts executing just fine.
However, I don't know how to get the data from the Model (which holds the grid data) into the Controller action. Can someone please help me out?
Here is the code for the View:
@model Telerik.Web.Mvc.GridModel<YeagerTech.YeagerTechWcfService.Customer>
@{
ViewBag.Title = "Customer Index";
}
<h2>
Customer Index</h2>
@( Html.Telerik().Grid<YeagerTech.YeagerTechWcfService.Customer>(Model.Data)
.Name("Customers")
.DataKeys(dataKeys => dataKeys.Add(o => o.CustomerID)
.RouteKey("CustomerID"))
.ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Text).ImageHtmlAttributes(new { style = "margin-left:0" }))
.Columns(columns =>
{
columns.Bound(o => o.CustomerID).Hidden(true);
columns.Command(commands =>
{
commands.Edit().ButtonType(GridButtonType.Text);
}).Width(200).Title("Command");
columns.Bound(o => o.Email).Width(200);
columns.Bound(o => o.Company).Width(200);
columns.Bound(o => o.FirstName).Width(100).Title("FName");
columns.Bound(o => o.LastName).Width(100).Title("LName");
columns.Bound(o => o.Address1).Width(200).Title("Addr1");
columns.Bound(o => o.Address2).Width(100).Title("Addr2");
columns.Bound(o => o.City).Width(100);
columns.Bound(o => o.State).Width(40).Title("ST");
columns.Bound(o => o.Zip).Width(60);
//columns.Bound(o => o.HomePhone).Width(120);
//columns.Bound(o => o.CellPhone).Width(120);
//columns.Bound(o => o.Website).Width(100);
//columns.Bound(o => o.IMAddress).Width(100);
//columns.Bound(o => o.CreatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120);
//columns.Bound(o => o.UpdatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120);
}).DataBinding(dataBinding =>
dataBinding.Ajax()
.Insert("_InsertAjaxEditing", "Customer")
.Update("_SaveAjaxEditing", "Customer"))
.Editable(editing => editing.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Scrollable()
)
Here is the code for the Controller:
[HttpPost]
[GridAction]
public ActionResult _SaveAjaxEditing()
{
YeagerTechWcfService.Customer cust = new YeagerTechWcfService.Customer();
if (TryUpdateModel(cust))
{
try
{
db.EditCustomer(cust); // This is a WCF method which works fine...
TempData["ErrCode"] = "Customer successfully updated.";
return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
TempData["ErrCode"] = "CustErr";
ViewBag.Error = ex.Message;
return View();
}
}
else
return View();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您获得的数据是绑定到网格的模型,并且您编辑的一个实体被发回。控制器可以声明如下:
I think the data you get is the model which is binded to the grid, and one entity which you edit is sent back. the controller could be declared as below: