Hello,
I'm looking at the "editing_custom" grid example for ASP.NET MVC and wondering if there is a way to bind the drop down without ViewData? I would like to use a generic ViewModel with Value and Text pairs similar to our existing MVC solution. I am asking this because there is no reusability when populating the CategoryViewModel and ViweData in the controller. Only the EditorTemplate can be reused.
@model Kendo.Mvc.Examples.Models.CategoryViewModel
@(Html.Kendo().DropDownListFor(m => m)
.DataValueField("CategoryID")
.DataTextField("CategoryName")
.BindTo((System.Collections.IEnumerable)ViewData["categories"])
)
Also, how do you add a "--Select a Category--" at the top of the dropdown list (in your example and in a non-ViewData approach)?
Thanks
John
10 Answers, 1 is accepted
It is possible to make the dropdown editor request the data when it is opened. The configuration for it would be very similar to the one in the example below:
Note that with this approach every time the cell is placed in edit mode the items will be requested from the server and that may have an impact on performance.
Regards,
Viktor Tachev
Progress Telerik
For showing a default text when there is no value selected in the DropDownList you can use the optionLabel option. Configuring it for the MVC wrapper would look like this:
.OptionLabel(
"Select an option"
)
Regards,
Viktor Tachev
Progress Telerik
Thanks, Victor. I see the option. But when I do "Add new record", it is defaulting to the first valid option instead of "Select an option". Is there a way to do that when adding a new record to an editable grid?
Controller:
var statuses = dataContext.Setup_EntityStatus
.Select(c => new EntityStatusVM
{
EntityStatusID = c.EntityStatusID,
EntityStatusDescription = c.EntityStatusDescription
})
.OrderBy(e => e.EntityStatusDescription);
ViewData["statuses"] = statuses;
ViewData["defaultStatus"] = statuses.First();
View:
model.Field(p => p.EntityStatus)
.DefaultValue(ViewData["defaultStatus"] as Verdant.ViewModels.Manage.EntityStatusVM);
Editor Template:
@(Html.Kendo().DropDownListFor(m => m)
.DataValueField("EntityStatusID")
.DataTextField("EntityStatusDescription")
.BindTo((System.Collections.IEnumerable)ViewData["statuses"])
.OptionLabel("Select an option")
When a new item is created the DefaultValue will be applied to the fields. In order for the DropDownList to show "Select an option" there should be no underlying value. Try changing the DefaultValue for the Grid like this and see how the behavior changes:
model.Field(p => p.EntityStatus).DefaultValue(
new
Verdant.ViewModels.Manage.EntityStatusVM());
Regards,
Viktor Tachev
Progress Telerik