4
votes

i have a angularJS controller where i put a kendo cascade dropdownlist.For dropdown list value ,on kendo dataSource read i am calling the web api service. For the first field the api GetDivisions() has been called and it response also but for the 2nd value the GetCascadeDistrict() method not called the GetDivisions() method called again. How can i solve this.Need Help

here's the angular Controller with kendo cascade dropdownlist.

app.controller("filterCtrl", function($scope, $sce,$http) {
var i;
$scope.dashImgSrc = $sce.trustAsResourceUrl('Content/Images/Bangladesh_Govt.gif');

$(document).ready(function () {

    var divisions = $("#divisions").kendoComboBox({
        filter: "contains",
        placeholder: "select a divisions...",
        dataTextField: "Name",
        dataValueField: "Id",
        animation: {
          close: {
              effects: "zoom:out",
              durations:250
          }  
        },
        dataSource: {
            type: "json",
            serverFiltering: true,
            transport: {
                read: "api/AreaService/GetDivisions()"
            }
        },
        change: function () {
            i = divisions.value();
            alert("1st hit"+i);
        }
    }).data("kendoComboBox");


    var districts = $("#districts").kendoComboBox({
        autoBind: false,
        cascadeFrom: "divisions",
        filter: "contains",
        placeholder: "select a district",
        dataTextField: "Name",
        dataValueField: "Id",
        dataSource: {
            type: "json",
            serverFiltering: true,
            transport: {
                read: function () {

                    alert("2nd hit");

                    //$http.get("/api/AreaService/GetCascadeDistrict(i)").success(function() {
                    //    alert("Hit the district api");
                    //}).error(function() {
                    //    alert("Error");
                    //});

                    $http({ method: "GET", url: 'api/AreaService/GetCascadeDistrict(i)' }).
                        success(function() {
                            alert("Actually it hit the custome get method");
                        }).
                        error(function() {
                            alert("Not hit or other problem");
                        });


                }
            }
        }


    }).data("kendoComboBox");


    var upazila = $("#upazila").kendoComboBox({
        autoBind: false,
        cascadeFrom: "districts",
        filter: "contains",
        placeholder: "select a  upazila...",
        dataTextField: "Name",
        dataValueField: "Id",
        dataSource: {
            type: "json",
            serverFiltering: true,
            transport: {
                read: function() {
                    $http.get("/api/AreaService/GetCascadeDistrict(i)").success(function() {

                    }).error(function() {

                    });
                }
            }
        }
    }).data("kendoComboBox");

    $("#get").click(function () {
        var divisionInfo = "\Division: { id: " + divisions.value() + ", name: " + divisions.text() + " }",
            districtInfo = "\nDistrict: { id: " + districts.value() + ", name: " + districts.text() + " }",
            upazilaInfo = "\nUpazila: { id: " + upazila.value() + ", name: " + upazila.text() + " }";

        alert("Road details:\n" + divisionInfo + districtInfo + upazilaInfo);
    });
});

});

And the Web api is here

public class AreaServiceController : ApiController
{
    private readonly AreaFilterManager _db = new AreaFilterManager();

    [System.Web.Http.HttpGet]
    public IEnumerable<Division> GetDivisions()
    {
        return _db.GetDivisions();
    }

    [System.Web.Http.HttpGet]
    public IEnumerable<District> GetCascadeDistrict(int? division)
    {
        return _db.GetCascadeDistrict(division);
    }

    [System.Web.Http.HttpGet]
    public IEnumerable<Thana> GetCascadeUpzilla(int? district)
    {
        return _db.GetCascadeThana(district);
    }

}
1

1 Answers

6
votes

You'll need to separate/distinguish your calls by CRUD operations or by Attribute Routing depends your WebApi version your using in your project.

You cannot use the same CRUD HttpGet twice in the same Class/Controller without putting a different routing attribute.

You need to remember, in WebApi the methods are not being called by their names like in regular programming, therefore the WebApi Class/Controller doesn't know which method you meant to call from your Client (in your case).

That's why you'll need to:

WebApi Ver 1 : separate/distinguish your calls by CRUD operations.

WebApi Ver 2 : separate/distinguish your calls by Attribute Routing.