2011-10-18 62 views
0

我正在嘗試使用網格中的記錄進行內聯編輯。使用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(); 
     } 
+1

後邏輯思考,我只是通過了如下模型... 公衆的ActionResult _SaveAjaxEditing(YeagerTechWcfService.Customer卡斯特) – sagesky36

+0

的內容,如果它工作了你可以添加它作爲你自己問題的答案,這樣可以幫助其他人。 –

回答

0

到網格,並且您編輯的一個實體被髮回。控制器可以如下聲明:

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