DropDownListFor SelectedIndex

2 posts, 0 answers
  1. Nathan
    Nathan avatar
    4 posts
    Member since:
    Oct 2016

    Posted 09 Nov Link to this post

    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?

  2. Ivan Danchev
    Admin
    Ivan Danchev avatar
    830 posts

    Posted 11 Nov Link to this post

    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.
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top