3

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 enter image description here

2
  • Just url: '@Url.Action("service", "delivery")', - you already sending the values in the data parameter (and your quoting the values of id, shopdoccode etc, but you cannot razor (server side code) and javascript variable (client side code) like that anyway) Commented Apr 11, 2017 at 22:01
  • And based on your error message - your script is in a separate file - razor code is not parsed in external files. Commented Apr 11, 2017 at 22:02

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but when I run the application and inspect what happens when I press the button: jquery-1.12.4.js:10254 POST localhost:63061/……0shopdoccode%20=%20%22shopdoccode%22,%20regdate%20=%20%22regdate%22%20%7D) 404 (Not Found)
Maybe post a screenshot, because that doesn't make much sense.
done ;) this happens when I inspect the browser and click on the buton
Is this javascript in the razor file? Or in a .js file? I'm guessing the latter because it doesn't make sense as to why @Url.Action would still be a part of the generated url.
2

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.

3 Comments

When I put it in my cshtml I get an error from new { id = "id", shopdoccode = "shopdoccode", regdate = "regdate" })', argument 3:cannot convert from '<anonymous type: string id, string shopdoccode, string regdate to string
Because you have submitted parameters in ajax inside, so do not need to set objectValue in Action
Similar to @Url.Action("ArticleDetail", "Article", new { id = item.ID }),The item.ID here is not from js.
2

test with Url.RouteUrl instead of Url.Action

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.