使用 Ajax 编辑 MVC 3 Razor Telerik 网格

发布于 2024-12-11 02:21:30 字数 3307 浏览 0 评论 0原文

我正在尝试对网格中的记录进行内联编辑。

单击“保存”按钮后,关联的控制器就开始正常执行。

但是,我不知道如何将数据从模型(保存网格数据)获取到控制器操作中。有人可以帮助我吗?

这是视图的代码:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

今天小雨转甜 2024-12-18 02:21:30

我认为您获得的数据是绑定到网格的模型,并且您编辑的一个实体被发回。控制器可以声明如下:

    [GridAction(EnableCustomBinding = true)]
    public ActionResult _SaveAjaxEditing(Customermodel, GridCommand command)

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:

    [GridAction(EnableCustomBinding = true)]
    public ActionResult _SaveAjaxEditing(Customermodel, GridCommand command)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文