2014-01-13 87 views
1

我有一個可編輯的Kendo UI網格並帶有自定義編輯器。每當我嘗試單擊自定義編輯器的文本框(這是當劍道是設置值到網格模型)之外,我得到一個JavaScript錯誤這樣的:使用自定義編輯器綁定網格子模型

0x800a138f - JavaScript runtime error: Unable to set property 'PercentWeight' of undefined or null reference

在調試程序時,例外情況:

function anonymous(d,value) { 
    d.PercentInfo.PercentWeight=value 
} 

當我將鼠標懸停在d,它除了PercentInfo我所有的IngredientViewModel特性,當我將鼠標懸停在PercentInfo,它是undefined

這裏是我的網格模型:

public class IngredientViewModel { 
    public string EncryptedIngredientId { get; set; } 
    public string CasNumber { get; set; } 
    public string IngredientName { get; set; } 

    [UIHint("PercentWeightEditor")] 
    public PercentInfoViewModel PercentInfo { get; set; } 
} 

這裏是IngredientViewModel使用的PercentInfoViewModel型號:

public class PercentInfoViewModel { 
    public decimal? PercentWeight { get; set; } 
    public string PercentUnit { get; set; } 
} 

這裏是電網部分的.cshtml:

@(Html.Kendo().Grid<IngredientViewModel>(Model) 
    .Name("IngredientsGrid") 
    .Editable(editable => editable.Mode(GridEditMode.InLine).Enabled(true)) 
    .BindTo(Model) 
    .DataSource(ds => ds 
     .Ajax() 
     .ServerOperation(false) 
     .Events(ev => ev.Error("onGridError")) 
     .Model(m => { 
       m.Id(p => p.EncryptedIngredientId); 
       m.Field(p => p.EncryptedIngredientId).Editable(false); 
       m.Field(p => p.PercentInfo); 
      }) 
     .Read("IngGrid_Read", "Company") 
     .Update("IngGrid_Update", "Company") 
     .Create("IngGrid_Create", "Company") 
     .Destroy("IngGrid_Destroy", "Company")) 
    .ToolBar(tbar => tbar.Create()) 
    .Columns(c => { 
       c.Bound(m => m.CasNumber); 
       c.Bound(m => m.IngredientName); 
       c.Bound(m => m.PercentInfo).ClientTemplate("#= PercentInfo.PercentWeight # #= PercentInfo.PercentUnit #"); 
       c.Command(command => { 
          command.Edit(); 
          command.Destroy(); 
         }).Width(200); 
      }) 
) 

這裏是PercentWeightEditor .cshtml:

@model SupplierPortalWebsite.Models.PercentInfoViewModel 

@Html.TextBoxFor(m => m.PercentWeight) 
@Html.TextBoxFor(m => m.PercentUnit) 

我在做什麼錯?

回答

0

我不得不修改網格設置的Model調用將空模型作爲默認值。下面是一塊新的代碼:

.Model(m => { 
      m.Id(p => p.EncryptedIngredientId); 
      m.Field(p => p.EncryptedIngredientId).Editable(false); 
      m.Field(p => p.PercentInfo) 
    /* I added this line --> */  .DefaultValue(new PercentInfoViewModel()); 
     }) 

這樣,總有它的自定義編輯器可以分配,而不是試圖分配給一個不存在的對象(這是什麼原因造成的性能,空模型。例外)。