简体   繁体   中英

Passing value to the controller as a parameter through ajax call

View

 function editEmployee(val) {
            var a = ko.toJSON(val);
           // alert("Hi");

            $.ajax({
                url: "@Url.Action("editEmployee", "Registration")",
                contentType: "application/json; charset=utf-8",
                type: "POST", dataType: "json",
                data:a,
                success: function (data) {
                  //  alert("awa");
                    debugger;
                    DisplayUI.getEmpArray(data);
                    var abc = DisplayUI.getEmpArray();


                }
            });
        }

Controller

 [HttpPost]
        public JsonResult editEmployee(string ID)
        {

            //JavaScriptSerializer serializer = new JavaScriptSerializer();
            //dynamic item = serializer.Deserialize<object>(ID);

            var employee = from s in db.Employee
                           select s;
            try
            {

                if (!String.IsNullOrEmpty(ID))
                {
                    employee = employee.Where(s => s.empId.Contains(ID));

                }

            }
            catch (Exception ex)
            {


            }

            var val = Json(employee.Select(s => new { s.empId, s.firstName, s.lastName, s.mobilePhn, s.email, s.desigId }).ToList());
            return val;



        }

Through ajax call I'm passing the value into controller as a string variable(ID). But that value is not passing it is visible as null value. I want to know get the value as a parameter what i should do.

Check whether you are getting json object

  var a = ko.toJSON(val); // here you should check whether you are getting json object 

Only if you get json object,the value will be bind to the controller.

put alert like this

var a = ko.toJSON(val);
alert(a);

if it is json object it will show like this

[object],[object]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM