MVC:AJAX 处的锚标记查询字符串未被控制器拾取

MVC: Anchor tag query strings at AJAX not pick up by Controller

我是 MVC 的新手,我来自 ASP 经典背景。我有以下 tableedit buttonDatatable 渲染:

当我点击 edit button 时,我希望将它们各自 AutoINC 的查询字符串传递给 Controller 并加载 PasswordDet 页面。

Javascript AJAX 编辑按钮的部分代码

"columns": [
                { "data": "LoginID", "orderable" : true },
                { "data": "Name", "orderable": true },
                { "data": "DateCreated", "orderable": true },
                {
                    "orderable":false,
                    "render": function (data, type, full, meta) {
                        return '<a href="/Password/PasswordDet/' + full.AutoINC + '"><img src="../../Content/myPics/edit-2-24.png" ></a>';
                    }
                },
            ],

控制器

我尝试了以下 withwithout 命名查询字符串

不命名查询字符串

public ActionResult PasswordDet(string AutoINC)
        {
            //**** AutoINC is always NULL****
            return View();
        }

用命名查询字符串

public ActionResult PasswordDet()
        {
            string AutoINC = Request.QueryString["AutoINC"];
            //**** AutoINC is always NULL****
            return View();
        }

此外,我有一个不传递任何查询字符串的新按钮,我该如何处理 NULL

"buttons": [
                {
                    text: 'New',
                    className: "btn btn-default",
                    action: function (e, dt, node, config) {
                        window.location.href = '@Url.Action("PasswordDet", "Password")';
                    },
                }
            ],

我假设您的默认路线如下所示:

routes.MapRoute(
   "Default",                                              // Route name
   "{controller}/{action}/{id}",                           // URL with parameters
   new { controller = "Home", action = "Index", id = UrlParameter.Optional}  // Parameter defaults
);

所以让你的PasswordDet操作方法如下:

public ActionResult PasswordDet(int id)
{
    // do whatever you want to with id here
    return View();
}

现在 localhost:50118/Password/PasswordDet/550 肯定会映射到具有 id550 的上述操作方法。