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

list not binding data

1 Answer 410 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Kahl
Top achievements
Rank 1
Kahl asked on 05 Dec 2016, 10:33 AM

I have a DDL  (below that is not binding to my controller (also below).   The contact controller/View are part of an area.

 

Please assist.

 

Thanks in advance

 

 

Controller

public ActionResult GetCountries([DataSourceRequest] DataSourceRequest dsRequest)
{
List<DropDownListModel> result = new List<DropDownListModel>();
foreach (DataRow dr in CommonWf.getLocationByTypeId(4).Tables[0].Rows)
{
if (!string.IsNullOrEmpty(dr["Name"].ToString()))
{
DropDownListModel returnModel = new DropDownListModel();
returnModel.Name = dr["Name"].ToString();
returnModel.Value = dr["LocationId"].ToString();
result.Add(returnModel);
}
}


DataSourceResult crapData = result.ToDataSourceResult(dsRequest);
return Json(crapData, JsonRequestBehavior.AllowGet);
}

 

 

Dropdown List

 

 

@(Html.Kendo().DropDownList()
.Name("CountryId")
.DataTextField("Name")
.DataValueField("Value")
.Events(e =>
{
e.Select("onChangeCountry"); //.DataBound("onCountryBound");
})
.OptionLabel("Select Country...")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetCountries", "Contact");
})
.ServerFiltering(true);
})

)

1 Answer, 1 is accepted

Sort by
0
Peter Milchev
Telerik team
answered on 07 Dec 2016, 09:50 AM
Hello Kahl,

The ToDataSourceResult() extension method modifies the structure of the result and the widget is not able to bind to it. You could find a sample configuration that uses the ToDataSourceResult() in Ajax Binding section of the MultiSelect's Overview article.

Here is a sample implementation of a DropDownList with Ajax binding:

public ActionResult GetProducts([DataSourceRequest] DataSourceRequest dsRequest)
{
    var northwind = new SampleEntities();
 
    var products = northwind.Products.Select(product => new ProductViewModel
    {
        ProductID = product.ProductID,
        ProductName = product.ProductName,
        UnitPrice = product.UnitPrice ?? 0,
        UnitsInStock = product.UnitsInStock ?? 0,
        UnitsOnOrder = product.UnitsOnOrder ?? 0,
        Discontinued = product.Discontinued
    });
 
    return Json(products.ToDataSourceResult(dsRequest), JsonRequestBehavior.AllowGet);
}

@(Html.Kendo().DropDownList()
    .Name("products")
    .DataTextField("ProductName")
    .DataValueField("ProductID")
    .OptionLabel("Select Product...")
    .DataSource(source =>
    {
        source
            .Custom()
            .Type("aspnetmvc-ajax") //Set this type if you want to use DataSourceRequest and ToDataSourceResult instances
            .Transport(tr => tr.Read(read =>
                {
                    read.Action("GetProducts", "DropDownList");
                })
            )
            .Schema(sc => sc
                .Data("Data") //define the [data] option
            )
            .ServerFiltering(true);
    })
)

Please find attached a sample application implementing an ajax bound DropDownList.

Regards,
Peter Milchev
Telerik by Progress
Telerik UI for ASP.NET MVC is ready for Visual Studio 2017 RC! Learn more.
Tags
DropDownList
Asked by
Kahl
Top achievements
Rank 1
Answers by
Peter Milchev
Telerik team
Share this question
or