简体   繁体   中英

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()

The C# controller:

     [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:

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. Unforntunately I can not simply use a hidden form for this.

Also this was quite helpful: Url.Action parameters?

@sleeyuen 在此处输入图片说明

Looks to me like your Url.Action has its parameters in the wrong order. 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.

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

You can not *.js or *.html file wrtie razor code.

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

The above code can only be used *.cshtml file.

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