I am creating a Telerik MVC UI grid and one of my properties in the record view model is a numeric company id.
What I want my grid to display is the display name for the company in the column when reading the grid, but when creating a new record (or editing an existing record) I would like it to show a drop down with possible values the user can select.
I've done the first part with c.Bound(x => x.CompanyId).ClientTemplate('#= getcompanyName(CompanyId)#');
and that works fine, but I don't see anything in the examples or API reference on how to have an editing template different than the display template.
I'm sure I am just missing something, since one example on Telerik's own website shows it (http://demos.telerik.com/aspnet-mvc/grid/editing-custom) but the source code they show is clearly missing the definition for the drop down box (easily seen in that their example passes up a categories
ViewData element but none of the provided source code actually uses that.
Any hints on what I"m missing to accomplish this?
Edit:
Ok, so after bashing my head against a wall I think what I want is to create a shared editor template, then do column.Bound(x => x.CompanyId).EditorTemplateName("IdNamePairDropdown").EditorViewData(new { IdNamePairs = Model.AvailableCompanies });
.
Of course this doesn't work, and gives me an ArgumentNullReference
exception even though at the break ViewData["IdNamePairs"]
gives me the correct non-null collection, but at least it's a hint.
Edit 2:
Looks like the solution is to do the following in your grid definition:
c.Bound(x => x.MarketingCompanyId).Title("Marketer")
.ClientTemplate("#= getCompanyName(MarketingCompanyId) #")
.EditorTemplateName("IdNamePairDropdown")
.EditorViewData(new { IdNamePairs = Model.AvailableCompanies, IdNamePairName = "marketing_companies" });
Then create the corrosponding Editor Template in ~/Views/Shared/EditorTemplates/IdNamePairDropdown.cshtml
@using System.Collections
@(Html.Kendo().DropDownList()
.Name("id_pair_dropdown_" + ViewData["IdNamePairName"])
.DataValueField("Id")
.DataTextField("Name")
.HtmlAttributes(new { data_value_primitive = "true"})
.BindTo((IEnumerable) ViewData["IdNamePairs"])))
This correctly allows a distinct drop down per column when in edit mode.
the only missing part is how to save the output, as selecting a value in the drop down and clicking the Update button returns the default value of the column.