Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am a beginner in MVC. Now i am trying to retrieve a list data from a controller. I am not getting any error while fetching the data. But its not working.

My code is,

Client Side

JavaScript
function ff_bind(drop_compny, drop_survey) {
        alert("test");
        debugger;
        $.ajax({
            type: "POST",
            url: "../ManageEmployeNumber/ff_bind_dept",
            contentType: "application/json; charset=utf-8",
            data: "{ 'comp_id': " + drop_compny + ",'surv_id':" + drop_survey + " }",            
            //data: { comp_id: drop_compny },
            dataType: "json",
            success: function (data) {
                debugger;
                alert(result)
            },
            error: function (response) {
                debugger;
                alert(response.d);
            }
        });
    }


server side

C#
public tblDepartment[] ff_bind_dept(int comp_id, int surv_id)
        {
           
            List<tblDepartment> lis_dept = new List<tblDepartment>();
            lis_dept = db.tblDepartments.Where(s => s.CompID == comp_id).ToList();

            return lis_dept.ToArray();
        }


Class

XML
public partial class tblDepartment
    {
        public decimal DeptID { get; set; }
        public Nullable<decimal> CompID { get; set; }
        public string DeptName { get; set; }
        public Nullable<decimal> NoEmployee { get; set; }
        public Nullable<double> TrgScore { get; set; }

        public virtual tblCompany tblCompany { get; set; }
    }



please help me...

thanks in advance..
Posted
Updated 17-Jul-15 0:36am
v2

1 solution

JavaScript
function ff_bind(drop_compny, drop_survey) {
        alert("test");
        debugger;
        $.ajax({
            type: "POST",
            data: "{ comp_id: '" + drop_compny + "',surv_id:'" + drop_survey + "' }",
            url: "../ManageEmployeNumber/ff_bind_dept",
            contentType: "application/json; charset=utf-8",
           //data: { comp_id: drop_compny },
            dataType: "json",
            success: function (data) {
                debugger;
                alert(result)
            },
            error: function (response) {
                debugger;
                alert(response.d);
            }
        });
    }
 
Share this answer
 
v3
Comments
Anil Vaghasiya 17-Jul-15 7:04am    
Hello,

have you use webapi for ff_bind_dept Methods??
vineeth raju 17-Jul-15 7:10am    
yes..
Actually ff_bind_dept function is working fine. but lis_dept.ToArray() value is not getting into success fieldof json. Instead of that its going to error and showing undefined.
Anil Vaghasiya 17-Jul-15 7:24am    
//Just Use this Method
function ff_bind(drop_compny, drop_survey) {
$http.post("http://localhost:16208/api/ManageEmployeNumber/ff_bind_dept", { headers: { 'data': '{ comp_id: '" + drop_compny + "',surv_id:'" + drop_survey + "' }','Content-Type': 'application/JSON', 'data-Type': 'JSON' } })
.success(function (response) {
alert(response);
});
}

//In WebAPI add Headers
[System.Web.Http.AcceptVerbs("GET", "POST")]
[System.Web.Http.HttpGet]
public tblDepartment[] ff_bind_dept(int comp_id, int surv_id)
{

List<tblDepartment> lis_dept = new List<tblDepartment>();
lis_dept = db.tblDepartments.Where(s => s.CompID == comp_id).ToList();

return lis_dept.ToArray();
}
vineeth raju 17-Jul-15 7:42am    
Which js is used for $http. Angular js?
Anil Vaghasiya 17-Jul-15 7:46am    
Yes I Used Angular JS and which You are Using and which you want??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900