简体   繁体   English

使用Ajax的网址操作参数

[英]Url action parameters using Ajax

I am trying to pass data from a view to a controller using parameters. 我正在尝试使用参数将数据从视图传递到控制器。 Now I am running a few difficulities. 现在我遇到了一些困难。 I am trying to pass those parameters once I select a row from a table and press on a button which has a onclick method to ShowTasks() 一旦我从表中选择一行并按下对ShowTasks()具有onclick方法的按钮,我就试图传递这些参数

The C# controller: C#控制器:

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

The Javascript function when user clicks on button: 用户单击按钮时的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");
                }
            });
        }
    }
}

What have I done so far? 到目前为止,我做了什么? Sitting for hours trying to find a way to give the parameters to my controller so I can invoke a SQL stored procedure. 坐了几个小时,试图找到一种将参数提供给控制器的方法,以便我可以调用SQL存储过程。 Unforntunately I can not simply use a hidden form for this. 不幸的是,我不能为此简单地使用隐藏形式。

Also this was quite helpful: Url.Action parameters? 这也很有帮助: Url.Action参数?

@sleeyuen @sleeyuen 在此处输入图片说明

Looks to me like your Url.Action has its parameters in the wrong order. 在我看来,您的Url.Action的参数顺序错误。 Change it to: 更改为:

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

Here's the appropriate overload that you want: 这是您想要的适当的重载:

Action(String, String, Object) with actionName, controllerName, and routeValues, in that order. 按此顺序具有actionName,controllerName和routeValues的Action(字符串,字符串,对象)

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

You can not *.js or *.html file wrtie razor code. 您不能* .js或* .html文件写剃刀代码。

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

The above code can only be used *.cshtml file. 上面的代码只能用于* .cshtml文件。

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

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