Supermode
Supermode

Reputation: 936

adding select value in kendo grid dropdownlist

Currently, if you choose select value option it would not select a value. Is there anyway i could add Select value in inline grid dropdownlist? when adding and editing record

GridForeignKey.cshtml

@(
 Html.Kendo().DropDownList()
        .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(""))
        .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
        .OptionLabel("Select value")
)

View:

 columns.ForeignKey(p => p.SSID, (System.Collections.IEnumerable)ViewData["dataStatus"], "OptID", "OptName");

controller:

 private void PopulateDataStatus()
        {
            ViewData["dataStatus"] = new HEntities().COpts
                       .Select(e => new COptModel
                       {

                           OptID = e.OptID,
                           OptName = e.OptName,
                           CTypeID = e.CTypeID

                       })
                       .Where(e => e.CTypeID == 99)
                       .OrderBy(e => e.OptName);
        }

So how i can add select value text then, please advise?

Upvotes: 0

Views: 5143

Answers (2)

Elsimer
Elsimer

Reputation: 1898

I've reread your question a couple of times now and I'm not sure if I understand it. If you're asking how to select a default value, then you want to use the SelectedIndex method, where the parameter is the index value (notice it's zero based). For instance, to make the second option your default (because it is zero-based):

@(
Html.Kendo().DropDownList()
  .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(""))
  .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
  .OptionLabel("Select value")
  .SelectedIndex(1)
)

Upvotes: 4

Petur Subev
Petur Subev

Reputation: 20233

OptionsLabel always has an empty value.

To change this and set value you need to add it as the first item with own text ("Select please") and value (the value you preffer) of the collection passed to the BindTo method (and remove the OptionLabel at all).

Upvotes: 1

Related Questions