使用 Ajax 的 URL 操作参数

2023-12-25

我正在尝试使用参数将数据从视图传递到控制器。 现在我遇到了一些困难。一旦我从表中选择一行并按下具有 ShowTasks() 的 onclick 方法的按钮,我就尝试传递这些参数

C# 控制器:

     [Route("/service/delivery/{id}/{shopdoccode}/{regdate}")]
     public ActionResult Delivery(string id, string shopdoccode, string regdate)
     {
        //do stuf
     }

用户点击按钮时的Javascript函数:

function ShowTasks() {
    //Dear Stackoverflow > This works, this is for selecting a row in the table
    var $selectedRow = $(".highlight");
    if ($selectedRow.length == 1) {
        var dcColumn = 0;
        var rdColumn = 1;
        var shopdoccodeColumn = 3;

        //assigning name to the colomn value 
        var id = $selectedRow[0].children[dcColumn].innerText.trim();
        var regdate = $selectedRow[0].children[rdColumn].innerText.trim();
        var shopdoccode = $selectedRow[0].children[shopdoccodeColumn].innerText.trim();

        //ajax 
        if (id && regdate && shopdoccode) {
            $.ajax({
                type: 'POST',
                url: '@Url.Action("service", "delivery" ,new { id = "id", shopdoccode = "shopdoccode", regdate = "regdate" })',
                data: { id, regdate, shopdoccode },
                success: function (data) {
                    if (data.success) {
                        console.log("Succes");
                    }

                },
                error: function (data) {
                    console.log("Error");
                }
            });
        }
    }
}

到目前为止我做了什么?我花了几个小时试图找到一种方法来将参数提供给我的控制器,以便我可以调用 SQL 存储过程。 不幸的是,我不能简单地使用隐藏表单来实现这一点。

这也很有帮助:Url.Action 参数? https://stackoverflow.com/questions/6278694/url-action-parameters

@sleeyuen enter image description here


在我看来就像你的Url.Action其参数顺序错误。将其更改为:

url: '@Url.Action("delivery", "service", new { id = "id", shopdoccode = "shopdoccode", regdate = "regdate" })',

这是您想要的适当的重载:

动作(字符串、字符串、对象) https://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action(v=vs.118).aspx#M:System.Web.Mvc.UrlHelper.Action%28System.String,System.String,System.Object%29按顺序包含actionName、controllerName 和routeValues。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Ajax 的 URL 操作参数 的相关文章

随机推荐