简体   繁体   中英

how to pass a variable in to controller using jquery ajax from url

I want to pass an id to a function in controller any suggestion. It shows cannot find the method in the controller

This is my view

function Delete1(id) {

        if (confirm("are u sure !")) {


            $.ajax({

                type: "POST",
                url: '/Register/Delete?id',
                dataType: 'JSON',
                success: function (result) {
                    if (result == "success") {
                        alert("Your account deleted successfully");
                        window.location.href = "/Register/Index";
                    }
                }

            });

        }

    }

This is my controller

  public JsonResult Delete(int id) {


        userTable x = db.userTables.Where(y => y.ID == id).FirstOrDefault<userTable>();
        db.userTables.Remove(x);
        db.SaveChanges();
        Session.Clear();
        Session.Abandon();
        return Json("success", JsonRequestBehavior.AllowGet);
    }

You need to pass the value using data property of ajax.Check this link

like this

$.ajax({
        url: "/Register/Delete",
        data: { id : 1 },
        type: "POST",
        success: function () {

        }
    });

您需要正确设置url:url:@ Url.Action(“ Delete”,“ ControllerName”)',并且您的代码应读取该值。

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