Thursday, October 15, 2015

Editor Template not binding to model on Grid Popup Save

I had a kendo grid that used popup edit mode.  On the popup editor template had a typical Razor @Html.EditorFor(model => model.fk_DrugBrandID)


When editing/adding a record, the editor correctly showed a drop down list of items, but on save, it was not updating the model.  Spent hours trying to figure it out, then realized I had encountered this before.  It is because the grid does not handle nullable foreign keys!  The solution is to use the Save Event of the grid to something like this:



 //grid doesn't handle nullable foreign keys, this passes the value to the controller  
   function tvyascoSave(e) {  
     /*alert('saving: ' + e.model.fk_DrugBrandID);*/  
     if (!e.model.fk_DrugBrandID) {  
       //change the model value  
       e.model.fk_DrugBrandID = 0;  
       //get the currently selected value from the DDL  
       var currentlySelectedValue = $(e.container.find('[data-role=dropdownlist]')[0]).data().kendoDropDownList.value();  
       //set the value to the model  
       e.model.set('fk_fk_DrugBrandID', currentlySelectedValue);  
     }  
   }  

No comments:

Post a Comment