Telerik Grid with Identical Property Names
I'm working on a medical application where a grid needs to display the ICD description as well as its associated HCC category description. The ICD and HCC types look like this:
public class ICD {
public String Code { get; set; }
public String Description { get; set; }
public HCC HCC { get; set; }
}
public class HCC {
public Int32 ID { get; set; }
public String Description { get; set; }
}
When I bind the Telerik MVC extensions grid to a list of ICD objects, I'm setting up the columns like so:
this.Html.Telerik().Grid(this.Model.ICDs)
.Name("ICDGrid")
开发者_如何学编程 .DataKeys(keys => keys.Add(icd => icd.Code))
.DataBinding(binding => {
binding.Ajax().Select(this.Model.AjaxSelectMethod);
binding.Ajax().Update(this.Model.AjaxUpdateMethod);
})
.Columns(columns => {
columns.Bound(icd => icd.ICDType.Name).Title("ICD 9/10");
columns.Bound(icd => icd.Code);
columns.Bound(icd => icd.Description);
columns.Bound(icd => icd.HCC.Description).Title("HCC Category")
columns.Command(commands => commands.Delete()).Title("Actions").Width(90);
})
.Editable(editing => editing.Mode(GridEditMode.InCell).DefaultDataItem(new ICD()))
.ToolBar(commands => {
commands.Insert();
commands.SubmitChanges();
})
.Sortable()
.Filterable()
.Pageable(paging => paging.PageSize(12))
.Render();
The problem is that both ICD and HCC have properties named "Description", and I have no control over that. Is there a way to tell Telerik to call them different things in the JavaScript it generates? Something like ICDDescription and HCCDescription?
Currently you cannot alias the properties. What you can do is create a ViewModel object with where the properties are named uniquely. Then bind the grid to the ViewModel object. Here is a code snippet:
public class ICDViewModel
{
public string Description
{
get;
set;
}
public string HCCDescription
{
get;
set;
}
// The rest of the properties of the original ICD class
}
Then you need to change the type of Model.ICDs to use ICDViewModel. You can use the Select
extension method to map ICD to ICDViewModel:
Model.ICDs = icds.Select(icd => new ICDViewModel
{
Description = icd.Description,
HCCDescription = icd.HCC.Description
/* set the rest of the ICD properties */
});
精彩评论