Home Passing data from javascript jquery to C# http post fails to work
Reply: 1

Passing data from javascript jquery to C# http post fails to work

Joshua Ngila
1#
Joshua Ngila Published in 2017-12-07 14:20:34Z

Am trying to pass data from jquery to asp.net mvc controller to get a response but fails to work

This is what i have tried

In jquery

 $('#Emp_Payroll_No').change(function () {
        var empdata = $(this).val();
        $.post('/tblHandOvers/GetEmployee', { payrollno: empdata}).then(function (res) {
            console.log("response is", res);  //res is always empty

        })
    })

In my c# code

        public JsonResult GetEmployee(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string passedtext = context.Request.Form["payrollno"].ToString();

        var PersonsQuery = db.vwEmployee_HandoverContact.Where(x => x.Emp_Payroll_No == passedtext);
        return Json(PersonsQuery);
    }

so am trying to pass payrollno

When using a manual value eg: EMP2001 in the controller it works like

        public JsonResult GetEmployee(HttpContext context)
     {
        var PersonsQuery = db.vwEmployee_HandoverContact
                             .Where(x => x.Emp_Payroll_No == "EMP2001 "); //passed manually
        return Json(PersonsQuery);
    }

How do i go about this?

lowry0031
2#
lowry0031 Reply to 2017-12-07 15:00:46Z

In your C#, I would update to this:

        public JsonResult GetEmployee(string payrollno)
{
    var PersonsQuery = db.vwEmployee_HandoverContact.Where(x => x.Emp_Payroll_No == payrollno);
    return Json(PersonsQuery);
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.330441 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO