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

DropDownListFor SelectedIndex

1 Answer 694 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Nathan
Top achievements
Rank 1
Nathan asked on 09 Nov 2016, 06:21 PM

I'm working with Kendo UI on an MVC application. We have a grid and when the user opens the row for editing we have a dropDownList that holds company names. I'm trying to get the DDL to default to the company name that's pertinent to the row.

Here's the column code:

columns.Bound(e => e.company_business_name).Width(220).Title("Company")
    .EditorTemplateName("CompanyName");

and here's the editorTemplate code:

 

@model  string
 
@(Html.Kendo().DropDownListFor(m => m)
        .DataTextField("Text")
        .DataValueField("Value")</p><p>
        .BindTo((System.Collections.IEnumerable)ViewData["Companies"])
 )

and the method that fills the DDL:

private void PopulateCompanies()
    {
        var companyList = new List<SelectListItem>();
 
        if (!string.IsNullOrEmpty(Session["Companies"] as string))
        {
            companyList = (List<SelectListItem>)Session["Companies"];
        }
        else
        {
            companyList = new DataAccess().GetCompanies(CurrentSettings.getUser().userId);
            CacheCompanies(companyList);
        }
 
        ViewData["Companies"] = companyList;

}

The dropDownList is populating, and displays when I click edit, but the selected value is blank.  How can I set it to the value that was in the grid when it was in display mode?

1 Answer, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 11 Nov 2016, 02:17 PM
Hello Nathan,

We would suggest you to check the explanation and example Igorrious provides in this thread, which discusses a similar scenarios. Make sure the DropDownList's Name matches the column's name (or if you prefer to use the DropDownListFor helper to set the model properly: DropDownListFor(m => m.company_business_name)

Regards,
Ivan Danchev
Telerik by Progress
Check out the new UI for ASP.NET Core, the most complete UI suite for ASP.NET Core development on the market, with 60+ tried-and-tested widgets, based on Kendo UI.
Tags
DropDownList
Asked by
Nathan
Top achievements
Rank 1
Answers by
Ivan Danchev
Telerik team
Share this question
or