Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing dynamic javascript values using Url.action()

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

like image 217
Kokila Avatar asked Feb 27 '13 12:02

Kokila


People also ask

How do you pass dynamic values in URL action?

How do you pass dynamic values in URL action? 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: var firstname = "abc"; var username = "abcd"; location. href = '@Url.

How do you call an action method with parameters in MVC?

There are a number of ways in which you can pass parameters to action methods in ASP.NET Core MVC. You can pass them via a URL, a query string, a request header, a request body, or even a form. This article talks about all of these ways, and illustrates them with code examples.


1 Answers

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:

var firstname = "abc"; var username = "abcd"; 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.

like image 186
Felipe Oriani Avatar answered Sep 18 '22 09:09

Felipe Oriani