77

Could anyone please tell how to pass dynamic values using Url.action().

Something Like,

var firstname="abc";
var username = "abcd";
location.href = '@Html.Raw(@Url.Action("Display", "Customer", new { uname = firstname ,name = username}))';

firstname, username is not getting reffered inside the Url.action() method.

How to pass these dynamic values using Url.action()?

0

4 Answers 4

168

The @Url.Action() method is proccess on the server-side, so you cannot pass a client-side value to this function as a parameter. You can concat the client-side variables with the server-side url generated by this method, which is a string on the output. Try something like this:

let firstName = "John";
let userName = "Smith";
location.href = '@Url.Action("Display", "Customer")?uname=' + firstName + '&name=' + userName ;

The @Url.Action("Display", "Customer") is processed on the server-side and the rest of the string is processed on the client-side, concatenating the result of the server-side method with the client-side.

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

2 Comments

I ended up adding an onclick event to a <a> element which appended to the href attribute which was set using Url.Action
But what if without that parameter the path is different?
11

This answer might not be 100% relevant to the question. But it does address the problem. I found this simple way of achieving this requirement. Code goes below:

<a href="@Url.Action("Display", "Customer")?custId={{cust.Id}}"></a>

In the above example {{cust.Id}} is an AngularJS variable. However one can replace it with a JavaScript variable.

I haven't tried passing multiple variables using this method but I'm hopeful that also can be appended to the Url if required.

Comments

2

The easiest way is:

  onClick= 'location.href="/controller/action/"+paramterValue'

1 Comment

do you really need an onclick event? could you do the same thing with the form's action attribute...
1

In my case it worked great just by doing the following:

The Controller:

[HttpPost]
public ActionResult DoSomething(int custNum)
{
    // Some magic code here...
}

Create the form with no action:

<form id="frmSomething" method="post">
    <div>
        <!-- Some magic html here... -->
    </div>
    <button id="btnSubmit" type="submit">Submit</button>
</form>

Set button click event to trigger submit after adding the action to the form:

var frmSomething= $("#frmSomething");
var btnSubmit= $("#btnSubmit");
var custNum = 100;

btnSubmit.click(function()
{
    frmSomething.attr("action", "/Home/DoSomething?custNum=" + custNum);

    btnSubmit.submit();
});

Hope this helps vatos!

1 Comment

Maybe it would be more future-proof if you could replace the string /Home/DoSomething with @Url.Action("DoSomething", "TheController"). That way if you rename your controller and/or action, your related url would still stay valid.

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.