DropDownList in Grid Cell

3 posts, 0 answers
  1. Ed
    Ed avatar
    3 posts
    Member since:
    Oct 2015

    Posted 08 Apr Link to this post

    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. Ed
    Ed avatar
    3 posts
    Member since:
    Oct 2015

    Posted 08 Apr Link to this post

    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.
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Ed
    Ed avatar
    3 posts
    Member since:
    Oct 2015

    Posted 08 Apr in reply to Ed Link to this post

    All fixed! Figures you figure it all out the second you post for help. lol That's the way it goes.
Back to Top