Search code examples
asp.net-mvckendo-asp.net-mvc

MVC 5 Entity Framework 6 project with Database first, ViewBag, dropdown for foreign key and databinding


I am experimenting with the Entity Framework in a new mvc project, so I created a database, and started out with a database first approach. One of the tables I created had a foreign key to another table, and when the model got created, a virtual property was created to address the key value. Then I had Visual studio create the controller / views with all the crud. Everything is working fine, but I want to change the dropdowns to Kendo. The Controller is using ViewBag properties to send the foreign key data back to the view like so:

    ViewBag.CourtId = new SelectList(db.Courts, "Id", "Name", tournament.CourtId);

the dropdown looks like this:

  @Html.DropDownList("ProviderId", null, new {@class = "form-control"})<br />

I can't figure out how the viewBag data is being bound to the dropdown, nor have I been able to figure out how to substitiute a kendo dropdownlist? How is this ViewBag data being bound to the dropdown?


Solution

  • so, I found the answer here: Click this Link Just sub out:

        @Html.DropDownList("ProviderId", "Select a Value")
    

    with

          @(Html.Kendo().DropDownListFor(model => model.ProviderId)
                .OptionLabel("Select a Value")
                .BindTo(ViewData["ProviderId"] as SelectList))