list not binding data

2 posts, 0 answers
  1. Kahl
    Kahl avatar
    15 posts
    Member since:
    Nov 2016

    Posted 05 Dec 2016 Link to this post

    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);
    })

    )

  2. Peter Milchev
    Admin
    Peter Milchev avatar
    236 posts

    Posted 07 Dec 2016 Link to this post

    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.
Back to Top