繁体   English   中英

使用Ajax的网址操作参数

[英]Url action parameters using Ajax

我正在尝试使用参数将数据从视图传递到控制器。 现在我遇到了一些困难。 一旦我从表中选择一行并按下对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参数?

@sleeyuen 在此处输入图片说明

在我看来,您的Url.Action的参数顺序错误。 更改为:

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

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

按此顺序具有actionName,controllerName和routeValues的Action(字符串,字符串,对象)

使用Url.RouteUrl而不是Url.Action进行测试

您不能* .js或* .html文件写剃刀代码。

@Url.Action(string actionName,string controllerName,object routeValues)

上面的代码只能用于* .cshtml文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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