DropDownList in Grid Cell

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

    Posted 08 Apr 2016 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:

                                    .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())
                                    .Pageable(pageable => pageable
                                    .DataSource(dataSource => dataSource
                                        .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):

        .OptionLabel("Select Action")

    Here's my model:

    public class AdHocLocationViewModel
            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; }
            public SelectListItem Action { get; set; }

  2. Ed
    Ed avatar
    3 posts
    Member since:
    Oct 2015

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

    Posted 08 Apr 2016 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