This is a migrated thread and some comments may be shown as answers.

Editable Grid DDL

10 Answers 86 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 15 Jan 2019, 12:38 AM

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

Sort by
0
Viktor Tachev
Telerik team
answered on 15 Jan 2019, 11:57 AM
Hi John,

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
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
John
Top achievements
Rank 1
answered on 15 Jan 2019, 12:49 PM
Thanks Viktor.  How can I get a "-- Select one--" at the top of the dropdownlist?  Does it work the same way using either approach (bound vs remote)?
0
John
Top achievements
Rank 1
answered on 15 Jan 2019, 12:50 PM
Thanks Viktor.  How can I get a "--Select one--" at the top of the dropdownlist?  Is the approach the same whether bound or remote?
0
John
Top achievements
Rank 1
answered on 15 Jan 2019, 12:50 PM
Thanks Viktor.  How can I get a "--Select one--" at the top of the dropdownlist?  Is the approach the same whether bound or remote?
0
John
Top achievements
Rank 1
answered on 15 Jan 2019, 12:51 PM
Thanks Viktor.  How can I get a "Select a Category" at the top of the dropdownlist?  Is the approach the same whether bound or remote?
0
John
Top achievements
Rank 1
answered on 15 Jan 2019, 12:54 PM
Thanks.  How can I get a "Select a Category" at the top of the dropdownlist?  Is the approach the same whether bound or remote?
0
John
Top achievements
Rank 1
answered on 15 Jan 2019, 12:56 PM
FYI The forum kept reporting an error so I kept trying to submit it again.
0
Viktor Tachev
Telerik team
answered on 17 Jan 2019, 08:59 AM
Hello John,

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
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
John
Top achievements
Rank 1
answered on 17 Jan 2019, 12:58 PM

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")

0
Viktor Tachev
Telerik team
answered on 21 Jan 2019, 08:41 AM
Hello John,

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
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
John
Top achievements
Rank 1
Share this question
or