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

DropDownList in Grid Cell

2 Answers 804 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ed
Top achievements
Rank 1
Ed asked on 08 Apr 2016, 04:49 PM

So, I've tried to get this working following many threads and articles and I can't get it to work. The value posts back to the controller fine, but I can't get it to set the default selected item on the DropDownList nor can I get it to display the text of the selected item when I select an option. If I use a client template to try and grab the text, it throws an error (Cannot read property 'Text' of null) and won't show any items in the grid. For this one, I'm not using just an IEnumerable list of ViewModels (like most of the examples show) to populate the DropDownList, I'm using a SelectList. I have it working fine in another project with an IEnumerable list of ViewModels, but I just can get it working with a SelectList and I don't want to create a ViewModel for these static options. Please help! Thanks!

Here's my grid:

@(Html.Kendo().Grid<Tracker.Areas.Admin.Models.AdHocLocationViewModel>()
                                .Name("gridAdHocLocations")
                                .Columns(columns =>
                                {
                                    columns.Bound(c => c.LocationName).Title("Reviewer Location");
                                    columns.Bound(c => c.Packet).Title("Added to");
                                    columns.Bound(c => c.CreateDt).Title("Date Added").Width(120).Format("{0:MM/dd/yyyy}");
                                    columns.Bound(c => c.Action).Title("Selection").ClientTemplate("#=Action.Text#").Width(180);
                                })
                                .Editable(e => e.Mode(GridEditMode.InCell))
                                .ToolBar(t => t.Save())
                                .Sortable()
                                .Pageable(pageable => pageable
                                    .Refresh(true)
                                    .PageSizes(true)
                                    .ButtonCount(5))
                                .DataSource(dataSource => dataSource
                                    .Ajax()
                                    .PageSize(20)
                                    .Batch(true)
                                    .ServerOperation(false)
                                    .Model(s =>
                                    {
                                        s.Id(model => model.LocationId);
                                        s.Field(f => f.LocationName).Editable(false);
                                        s.Field(f => f.Packet).Editable(false);
                                        s.Field(f => f.CreateDt).Editable(false);
                                        s.Field(p => p.Action).DefaultValue(
                                            ViewData["defaultAction"] as SelectListItem);
                                    })
                                    .Read(read => read.Action("LocationsAdHoc_Read", "Location"))
                                    .Update(update => update.Action("LocationsAdHoc_Update", "Location"))
                                )
                            )

Here's my editor (I've also tried it strongly typed using DropDownListFor):

@(Html.Kendo().DropDownList()
    .Name("Action")
    .DataValueField("Value")
    .DataTextField("Text")
    .OptionLabel("Select Action")
    .BindTo((SelectList)ViewData["actions"])
)

Here's my model:

public class AdHocLocationViewModel
    {
        [ScaffoldColumn(false)]
        public int LocationId { get; set; }
        public string LocationName { get; set; }
        public string Packet { get; set; }
        public int ActionId { get; set; }
        public DateTime CreateDt { get; set; }
 
        [UIHint("AdHocLocationEditor")]
        public SelectListItem Action { get; set; }
    }

2 Answers, 1 is accepted

Sort by
0
Ed
Top achievements
Rank 1
answered on 08 Apr 2016, 04:54 PM
Never mind, I just realized I forgot to initialize my Action property in my model and that's why it was null. Doh! It all looks to be working now except setting the initial default value.
0
Ed
Top achievements
Rank 1
answered on 08 Apr 2016, 05:14 PM
All fixed! Figures you figure it all out the second you post for help. lol That's the way it goes.
Tags
Grid
Asked by
Ed
Top achievements
Rank 1
Answers by
Ed
Top achievements
Rank 1
Share this question
or