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

Dropdownlist on Grid Edit - Default selected value

3 Answers 1329 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Priya
Top achievements
Rank 1
Priya asked on 28 Dec 2015, 07:46 PM

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

 

 

 

3 Answers, 1 is accepted

Sort by
-1
Raja
Top achievements
Rank 1
answered on 28 Dec 2015, 08:05 PM

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

0
Priya
Top achievements
Rank 1
answered on 28 Dec 2015, 11:25 PM

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

-1
Raja
Top achievements
Rank 1
answered on 29 Dec 2015, 08:45 PM

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.

Tags
Grid
Asked by
Priya
Top achievements
Rank 1
Answers by
Raja
Top achievements
Rank 1
Priya
Top achievements
Rank 1
Share this question
or