2013-04-17 66 views
1

我使用一個網格與自定義編輯器。我用colum內的textarea替換常見的輸入。劍道網格取消按鈕丟失新值

USECASE:

  1. 焯芬textarea的電流值作爲 「測試0」
  2. 一格的
  3. 按下編輯鍵(它的行)
  4. 添加新的價值 「測試01」
  5. 新聞網格更新按鈕
  6. 檢查網格行中更新的新值(工作)
  7. 再次按網格編輯按鈕(新值爲「test 01」)
  8. 按取消按鈕的網格(這裏有一些魔術heppens!並且不僅包含網格,而且包含每個定製編輯元素)
  9. 檢查當前值是否從「測試01」變爲「測試0」(失敗)。

取消之前的值(「測試0」而不是「測試01」)通過網格顯示。 這種情況與來自其他列(不是customEditable)的值相同。量。如果我保存,然後按取消,則顯示錯誤的值,但正確的價值觀會在數據庫保存

滿格重裝節省情況,但它不是最佳的AJAX電網

網格的數據源:

// bind json result from /Bonuses/GetPagedJsonBonuses 
var bonusesDataSource = new kendo.data.DataSource({ 
    transport: { 
     read: "@Url.Action("GetPagedJsonBonuses", "Bonuses")", 
     update: { 
      url: "@Url.Action("Edit", "Bonuses")", 
      type: "PUT" 
     }, 
     create: { 
      url: "@Url.Action("Create", "Bonuses")", 
      type: "POST" 
     }, 
     parameterMap: function(options, operation) { 
      if (operation === "update" || operation === "create") { 
       // updates the BonusDTO.EmployeeId with selected value 
       if (newValueEmployeeId !== undefined) 
        options.EmployeeId = newValueEmployeeId; 
      } 
      return options; 
     } 
    }, 
    schema: { 
     data: "Data", // PagedResponse.Data 
     total: "TotalCount", // PagedResponse.TotalCount 
     model: { 
      id: "BonusId", // Data 
      fields: { 
       EmployeeId: { type: "number" }, 
       EmployeeLastName: { 
        type: "string", 
        editable: true, 
        //validation: { required: {message: "Employee's last name is required"}} 
       }, 
       Amount: { 
        type: "number", 
        editable: true, 
        nullable: false, 
        validation: { 
         required: { message: "Amount is required to be set" } 
        } 
       } 
      } // fields 
     } // model 
    }// schema 
}); 

Grid元素看起來是這樣的:

// creates bonuses grid control 
$("#bonusesGrid").kendoGrid({ 
    dataSource: bonusesDataSource, 
    toolbar: ["create"], 
    editable: "inline", 
    columns: [ 
     "BonusId", 
     "EmployeeId", 
     { 
      field: "EmployeeLastName", 
      editor: employeeAutocompletingEditor, 
      template: "#=EmployeeLastName#" 
     }, 
     "Amount", 
     { 
      field: "Comment", 
      titel: "Comment", 
      editor: textareaEditor, 
      filterable: { 
       operators: { 
        number: { 
         contains: "Contains" 
        } 
       } 
      } 
     }, 
     { 
      command: ["edit"], 
      title: " " 
     } 
    ], 
    save: function(e) { 
     if (newValueEmployeeId !== undefined && newValueEmployeeLastName !== undefined) { 
      e.model.EmployeeId = newValueEmployeeId; // it's a hack to bind model and autocomplete control 
      e.model.EmployeeLastName = newValueEmployeeLastName; 
     } 
    }, 
    edit: function(e) { 
     setCurrentValueEmployeeIdAndLastName(e.model.EmployeeId, e.model.EmployeeLastName); 
    }, 
    cancel: function(e) { 
     setCurrentValueEmployeeIdAndLastName(e.model.EmployeeId, e.model.EmployeeLastName); 
    } 
}); 

textarea的代碼是在這裏:

function textareaEditor(container, options) { 
    $('<textarea data-bind="value: ' + options.field + '" cols="10" rows="4"></textarea>') 
     .appendTo(container); 
} 

UPDATE 1

後步驟4(變化 '測試0' 至 '測試01' 註釋並按更新) 我有下一個請求:

Accept:application/json, text/javascript, */*; q=0.01 
    Content-Length:490 
    Content-Type:application/x-www-form-urlencoded; charset=UTF-8 
    Host:localhost 
    Origin:http://localhost 
    Referer:http://localhost/ 
    X-Requested-With:XMLHttpRequest 

機構POST

Form Dataview sourceview URL encoded 
    BonusId:45 
    EmployeeId:47 
    Employee[EmployeeId]:47 
    Employee[UserName]:opetriv 
    Employee[LastName]:Oleh Petrivskyy 
    Employee[LastNameUkr]:Петрівський Олег Миронович 
    EmployeeLastName:Oleh Petrivskyy 
    Date:Fri Apr 19 2013 12:00:00 
    Amount:2 
    **Comment:test 01** 
    IsActive:true 
    Ulc:ryakh    
    Dlc:Fri Apr 19 2013 12:34:33 GMT+0300 (FLE Daylight Time) 

它是正確的至於我。

來自控制器的響應是:

{"BonusId":45, 
"EmployeeId":47, 
"Employee":{"EmployeeId":47,"UserName":"opetriv","LastName":"Oleh  Petrivskyy","LastNameUkr":"Петрівський Олег Миронович"}, 
"EmployeeLastName":"Oleh  Petrivskyy","Date":"\/Date(1366362000000)\/","Amount":2,**"Comment":"test 01"**,"IsActive":true,"Ulc":"ryakh    ","Dlc":"\/Date(1366375264603)\/"} 

步驟7(按取消和接收此魔法)沒有任何AJAX請求到服務器之後。 我有一個數據源的錯誤處理程序,它不會觸發。

+0

你如何更新服務器中的值?你在'schema.model'中定義了一個'id'嗎?是否有可能共享編輯原始文章或JSFiddle的一些代碼? – OnaBai

+0

是的,我在我的模型中加入了一個id。代碼shered,對不起,但它是不可能的使用JSFiddle來模擬它 –

回答

2

當您按更新時,服務器的響應很重要。把數據源的錯誤事件的處理程序,看看它是否被觸發。

檢查服務器對更新請求的原始響應是什麼。它不應該包含任何錯誤。另外請記住,最新版本的jQuery空響應是無效的JSON,並且會觸發錯誤事件。

1

使用自定義編輯器更改字段不會觸發數據庫更新。這是可以解決的改變save功能如下:

save: function(e) { 
    if (newValueEmployeeId !== undefined && newValueEmployeeLastName !== undefined) { 
     e.model.EmployeeId = newValueEmployeeId; // it's a hack to bind model and autocomplete control 
    e.model.EmployeeLastName = newValueEmployeeLastName; 
    **e.model.dirty = true;** 
    } 
-1

我有同樣的問題。 在我的情況下,問題是,創建新的值後,我沒有返回適當的模型(有效的ID)。

舊代碼(沒有工作):

public ActionResult Product_Create([DataSourceRequest] DataSourceRequest request, Product product) 
{ 
    if (product != null && ModelState.IsValid) 
    { 
     Database.Products.InsertOnSubmit(new Product() 
     { 
      Name = product.Name, 
      IsActive = product.IsActive 
     }); 
     Database.SubmitChanges(); 
    } 

    return Json(new[] { product }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet); 
} 

新代碼:

public ActionResult Product_Create([DataSourceRequest] DataSourceRequest request, Product product) 
{ 
    Product newProduct = product; 
    if (product != null && ModelState.IsValid) 
    { 
     Database.Products.InsertOnSubmit(newProduct = new Product() 
     { 
      Name = product.Name, 
      IsActive = product.IsActive 
     }); 
     Database.SubmitChanges(); 
    } 

    return Json(new[] { newProduct }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet); 
} 

我使用ASP.NET MVC與EF所以Database.SubmitChanges()後我有有效身份證件產品。

+0

你正在使用mvc付費版本,我正在使用免費的Javascrtipt kendo UI版本 –