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

DropDownList stays empty

1 Answer 96 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Valid Development Factory
Top achievements
Rank 1
Valid Development Factory asked on 23 Jul 2017, 01:37 PM

I've tried using Ajax Binding (and also ToDataSourceResult Binding) from this article to populate a DropDownList. I copy/pasted the exact code from the article into my solution. The GetProducts method on the controller returns 78 products, yet the DropDownList stays empty.

What am I doing wrong?

Controller

public JsonResult GetProducts()
{
    NorthwindDataContext northwind = new NorthwindDataContext();
 
    return Json(northwind.Products, JsonRequestBehavior.AllowGet);
}

 

View

@(Html.Kendo().DropDownList()
    .Name("productDropDownList")
    .DataTextField("ProductName")
    .DataValueField("ProductID")
    .DataSource(source =>
    {
           source.Read(read =>
           {
                read.Action("GetProducts", "Home");
           })
           .ServerFiltering(true);
    })
    .SelectedIndex(0)
)

1 Answer, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 25 Jul 2017, 11:48 AM
Hello Teun,

If you had checked the "Include foreign key columns in the model" option form the Entity Data Model Wizard, you would have generated these properties in the model as demonstrated in this image:

https://monosnap.com/file/nLJozOufQvQ44EkWZPqk4PrukTCHzS


Hence a circular call to the object could appear (probably a js error with this information is thrown in your browser console). In order to overcome this, you can avoid checking the above described option, or you can have extracted only the needed part from your db context, using an anonymous object:

public JsonResult GetProducts()
{
    NorthwindDataContext northwind = new NorthwindDataContext ();
 
    var result = northwind.Products.Select(m => new
    {
        ProductID = m.ProductID,
        ProductName = m.ProductName
    });
 
    return Json(result, JsonRequestBehavior.AllowGet);
}

Another option (and preferable) is to have an ViewModel, linked to the model generated to mirror your tables e.g. ProductsViewModel. More information on that usage could be found below:

https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

Hope this information helps.

Regards,
Nencho
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
DropDownList
Asked by
Valid Development Factory
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Share this question
or