Image in Kendo Grid Dropdown List

In this blog, I would demonstrate how to insert a image in dropdown list (inside kendo grid) based on Type (CrewId) also this will help us to how to define the foreign key column in kendo Grid.

In this example, the grid will contain 3 columns a) Id b) CrewId [Man/Machine which is foreign key column] 3).

Comments

Let's define below Kendo Grid in Index.cshtml of HomeController. This is a basic kendo grid configuration with 2 columns as Bound columns and another column as ForeignKey Column which contains CrewID and we have list of CreworEquipment data which is stored in ViewState.

  1. @  
  2. (Html.Kendo().Grid < UserMachine > ()  
  3.     .Name("MyGrid")  
  4.     .HtmlAttributes(new {  
  5.         style = "height:100%; width:100%; "  
  6.     })  
  7.     .Columns(columns => {  
  8.         columns.Bound(m => m.Id).Hidden(true);  
  9.         columns.ForeignKey(m => m.CrewId, (System.Collections.IEnumerable) ViewData["CrewOrEquipment"], "Id""Name").Title("User/Equipment").Width(100);  
  10.         columns.Bound(m => m.Comments).Width(175).Title("Comments");  
  11.     })  
  12.     .ToolBar(toolbar => toolbar.Template( < text >  
  13.             < a class = "k-button k-button-icontext k-grid-add anch-show-hide" > < span class = "k-icon k-add" > < /span></a >  
  14.             < a class = "k-button k-button-icontext anch-show-hide"  
  15.             id = "cancelAct1"  
  16.             href = "javascript:void(0)" > < span class = "k-icon k-cancel" > < /span></a >  
  17.             < /text>))  
  18.             .Scrollable()  
  19.             .Editable(editable => editable.Mode(GridEditMode.InCell))  
  20.             .Filterable()  
  21.             .Sortable()  
  22.             .DataSource(dataSource => dataSource  
  23.                 .Ajax()  
  24.                 .Batch(true)  
  25.                 .ServerOperation(false)  
  26.                 .Model(model => {  
  27.                     model.Id(row => row.Id);  
  28.                 })  
  29.                 .ServerOperation(false)  
  30.                 .PageSize(25)  
  31.             )  
  32.             .Resizable(resize => resize.Columns(true))  
  33.             .Selectable(selectable => selectable.Mode(GridSelectionMode.Multiple))  
The major part of the code is the Grid Foreign Key template which would be defined in Shared folder. Lets add file GridForeignKey.cshtml inside folder View ==> Shared ==> EditorTemplate and add the below code to the file.
  1. @(Html.Kendo().DropDownListFor(m => m).DataValueField("Id").DataTextField("Name")  
  2. .BindTo((System.Collections.IEnumerable)ViewData["CrewOrEquipment"])  
  3. .TemplateId("contact-template"))  
Now let's define below template, and this is pretty straightforward based on Id, we are setting the respective Image along with data to it.
  1. <script id="contact-template" type="text/x-kendo-template">  
  2.     # if(data.CrewType == 1) { #  
  3.     <img alt="icon" class="k-image" src="http://icons.iconarchive.com/icons/icons-land/vista-people/16/Office-Customer-Male-Light-icon.png"> #: data.Name # # } # # if(data.CrewType == 2) { #  
  4.     <img alt="icon" class="k-image" src="http://icons.iconarchive.com/icons/iconshock/real-vista-construction/16/hand-driller-icon.png"> #: data.Name # # } #  
  5. </script>  
Finally let's define the Controller action Index with following code. As you can see we are using the ViewData in our ForeignKey column.
  1. public ActionResult Index()  
  2. {  
  3.    ViewData["CrewOrEquipment"] = GetCrewOrEquipment();  
  4.    return View();  
  5. }  
Let's run the application and once page loads, click the Plus button and now you select Crew Columns and you should be seeing the Images (crew/equipment images) based on type.