Dropdownlist on Grid Edit - Default selected value

4 posts, 0 answers
  1. Priya
    Priya avatar
    3 posts
    Member since:
    Dec 2015

    Posted 28 Dec 2015 Link to this post

    I have a grid with an InCell edit and I have one of the columns as a dropdown list and I'm implementing this using EditorTemplateName. I'm getting the dropdown list populated but how do I have the default value of the dropdown selected to the bound column value? Now I just get a dropdown on clicking the cell with all the values populated and the default selected value is blank.

     My Editior Template :

     

    @model IEnumerable<AMCUpfrontTracker2.Models.Agency_Ref>

    @(Html.Kendo().DropDownListFor(m => m)
            .DataValueField("AgencyID")
            .DataTextField("AgencyName")
            .BindTo((System.Collections.IEnumerable)ViewData["Agencies"])
    )

     

     

     

  2. Raja
    Raja avatar
    43 posts
    Member since:
    Oct 2013

    Posted 28 Dec 2015 in reply to Priya Link to this post

    I do this by assigning OptionLabel if I want to show a static text. Another way is to set the DefaultValue for the grid datasource.

    Editor Template:

     

    @(Html.Kendo().DropDownListFor(m=>m)
                .DataTextField("Text")
                .DataValueField("Value")
                .OptionLabel("Select option")
                .DataSource(dataSource => dataSource
                    .Read("GetValues", "Controller")
                )
    )

    Grid:

    @(Html.Kendo().Grid<Model>()
                .Name("Name")
                .Columns(columns =>
                {
                    columns.Bound(c => c.DropDown);
                })
                .DataSource(source => source
                    .Ajax()
                    .Model(model =>
                    {
                        model.Field(f => f.DropDown).DefaultValue(@Model.DropdownValue);
                    })
                    .Read(read => read.Action("Read", "Controller"))
                )
    )

  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Priya
    Priya avatar
    3 posts
    Member since:
    Dec 2015

    Posted 28 Dec 2015 in reply to Raja Link to this post

    Raja - Thank you for your response. I'm really new to Telerik and Kendo UI and I didn't quite follow the Default Value option in the grid. What does @Model.DropdownValue indicate?

     Here is my grid:

        @(Html.Kendo().Grid<RegistrationViewModel>()
          .Name("kGrid")
          .Columns(columns =>
          {
              columns.Bound(c => c.RegistrationID).Visible(false);
              columns.Bound(c => c.Accounts.AgencyName).EditorTemplateName("AgencyList").Width(250);
              columns.Bound(c => c.Accounts.AdvertiserName).Width(250);
              columns.Bound(c => c.RegistrationName).Width(250);
              columns.Bound(c => c.Networks.Network).Width(250);
              columns.Command(command => command.Destroy()).Width(200);
          })
           .ToolBar(toolBar =>
           {
               toolBar.Create();
               toolBar.Save();
           })
          .Editable(editable => editable.Mode(GridEditMode.InCell))
          .Scrollable()
          .Pageable(pageable => pageable
                .Refresh(true)
                .PageSizes(new List<object> {10,20,50,"All"})
                .ButtonCount(5))
          .Sortable()
          .Filterable()
          .ColumnMenu()
          .HtmlAttributes(new { style = "height:600px;" })
          .DataSource(dataSource => dataSource
              .Ajax()
             .Model(model =>
             {
             model.Id(p => p.AccountID);
             model.Field(p => p.AccountID).Editable(false);
             model.Field(p => p.Accounts.AgencyName);

        })
              .Read(read => read.Action("Accounts_Read", "Accounts").Data("additionalInfo"))
              .Create(create => create.Action("Accounts_Create", "Accounts"))
              .Update(update => update.Action("Accounts_Update", "Accounts"))
              .Destroy(destroy => destroy.Action("Accounts_Destroy", "Accounts"))
          )
        )

  5. Raja
    Raja avatar
    43 posts
    Member since:
    Oct 2013

    Posted 29 Dec 2015 in reply to Priya Link to this post

    Priya,

     

    Default value is the value of the item that you want to get selected by default. I your case it will be the AgencyID of the item to be selected. You can set that to you Model in your controller or you can use ViewBag or any other method you are comfortable with.

Back to Top
UI for ASP.NET MVC is VS 2017 Ready