In my scenario I need to load a dropdownlist with data that is determined at runtime.
In my case I have a grid filed with a single product's master data. Each row in the Kendo grid contains an attribute name and its associated value.
ex. "Product Name" "Ibuprophen"
"Strength" "200 mg"
"Form" "Casule"
Each attribute of the product comes from a different master data table, so what I was hoping to do was pass an entire object containing the information I need to dynamically load the dropdownlist to the editor template like so:
@model IEnumerable<NewGlobalProductCatalogue.Models.ProductInfo>
<p>Product Info - @ViewBag.ProductLanguage</p>
@{ string lang = ViewBag.ProductLanguage; }
@(Html.Kendo().Grid(Model)
.Name("ProductInfo_" + lang)
.Columns(columns =>
{
columns.Bound(c => c.ColumnName);
columns.Bound(c => c.ProductData.FieldName);
columns.Bound(c => c.ProductData).ClientTemplate("#: ProductData.MasterDataValue
#").EditorTemplateName("EditorProductMasterData"); <<--this is the important line... ProductData is a class with all the data I need to look up the product master data
columns.Command(command => { command.Edit(); });
})
.DataSource(d => d
.Ajax()
.Read(r => r.Action("Get", "Product"))
.Model(m =>
{
m.Id(p => p.ProductData.FieldID);
m.Field(p => p.ColumnName).Editable(false);
m.Field(p => p.ProductData.FieldName).Editable(false);
m.Field(p => p.ProductData.MasterDataValue).Editable(true);
})
.Update(u => u.Action("Update", "Product"))
)
.Pageable()
.Editable(e => e.Mode(GridEditMode.InLine))
And then in the editor I would pass the necessary parameters on to the .read action so that the controller would dynamically load the listbox from the correct table. (NOTE: All the master data tables have a predictable field structure so I simply call a dynamic stored procedure which builds the appropriate SQL query)
Here was my editor:
@model NewGlobalProductCatalogue.Models.ProductMasterData
@(Html.Kendo().DropDownList()
.Name("MasterDataValues")
.DataValueField("MasterDataID")
.DataTextField("MasterDataValue")
.DataSource(d => d
.Read(r => r.Action("Index", "MasterDataSelection", new { countrycode = Model.Country, lang = Model.Language, GlobalFieldName = Model.FieldName, MDTableName = Model.MasterDataTableName }))
)
.SelectedIndex(0)
)
Of course it all came crashing down when I realized that the editor template is only called once when the grid is built. The weird part is, the controller that loads the data IS called every time I click on a rows EDIT button.
public class MasterDataSelectionController : Controller
{
//
// GET: /MasterDataSelection/
private APOIPCEntities db = new APOIPCEntities();
public JsonResult Index(string countrycode, string lang, string GlobalFieldName, string MDTableName)
{
return this.Json(db.GetCountryLanguageMasterData(countrycode,lang,GlobalFieldName, MDTableName), JsonRequestBehavior.AllowGet);
}
}
I am wondering if someone can think of a sneaky way this behavior could be exploited to achieve the dynamic data loading I am looking for. Maybe if I could put a function call in my controller that could look back into the grid to see what row it is currently focused on, and extract the necessary data from it prior to calling the stored procedure ..... just not sure how to do that.
Thanks for any thoughts you might have.
-Sheldon
In my case I have a grid filed with a single product's master data. Each row in the Kendo grid contains an attribute name and its associated value.
ex. "Product Name" "Ibuprophen"
"Strength" "200 mg"
"Form" "Casule"
Each attribute of the product comes from a different master data table, so what I was hoping to do was pass an entire object containing the information I need to dynamically load the dropdownlist to the editor template like so:
@model IEnumerable<NewGlobalProductCatalogue.Models.ProductInfo>
<p>Product Info - @ViewBag.ProductLanguage</p>
@{ string lang = ViewBag.ProductLanguage; }
@(Html.Kendo().Grid(Model)
.Name("ProductInfo_" + lang)
.Columns(columns =>
{
columns.Bound(c => c.ColumnName);
columns.Bound(c => c.ProductData.FieldName);
columns.Bound(c => c.ProductData).ClientTemplate("#: ProductData.MasterDataValue
#").EditorTemplateName("EditorProductMasterData"); <<--this is the important line... ProductData is a class with all the data I need to look up the product master data
columns.Command(command => { command.Edit(); });
})
.DataSource(d => d
.Ajax()
.Read(r => r.Action("Get", "Product"))
.Model(m =>
{
m.Id(p => p.ProductData.FieldID);
m.Field(p => p.ColumnName).Editable(false);
m.Field(p => p.ProductData.FieldName).Editable(false);
m.Field(p => p.ProductData.MasterDataValue).Editable(true);
})
.Update(u => u.Action("Update", "Product"))
)
.Pageable()
.Editable(e => e.Mode(GridEditMode.InLine))
And then in the editor I would pass the necessary parameters on to the .read action so that the controller would dynamically load the listbox from the correct table. (NOTE: All the master data tables have a predictable field structure so I simply call a dynamic stored procedure which builds the appropriate SQL query)
Here was my editor:
@model NewGlobalProductCatalogue.Models.ProductMasterData
@(Html.Kendo().DropDownList()
.Name("MasterDataValues")
.DataValueField("MasterDataID")
.DataTextField("MasterDataValue")
.DataSource(d => d
.Read(r => r.Action("Index", "MasterDataSelection", new { countrycode = Model.Country, lang = Model.Language, GlobalFieldName = Model.FieldName, MDTableName = Model.MasterDataTableName }))
)
.SelectedIndex(0)
)
Of course it all came crashing down when I realized that the editor template is only called once when the grid is built. The weird part is, the controller that loads the data IS called every time I click on a rows EDIT button.
public class MasterDataSelectionController : Controller
{
//
// GET: /MasterDataSelection/
private APOIPCEntities db = new APOIPCEntities();
public JsonResult Index(string countrycode, string lang, string GlobalFieldName, string MDTableName)
{
return this.Json(db.GetCountryLanguageMasterData(countrycode,lang,GlobalFieldName, MDTableName), JsonRequestBehavior.AllowGet);
}
}
I am wondering if someone can think of a sneaky way this behavior could be exploited to achieve the dynamic data loading I am looking for. Maybe if I could put a function call in my controller that could look back into the grid to see what row it is currently focused on, and extract the necessary data from it prior to calling the stored procedure ..... just not sure how to do that.
Thanks for any thoughts you might have.
-Sheldon