Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass parameter from @Url.Action to controller function

Tags:

c#

asp.net-mvc

I have a function CreatePerson(int id) , I want to pass id from @Url.Action.

Below is the reference code:

public ActionResult CreatePerson(int id) //controller  window.location.href = "@Url.Action("CreatePerson", "Person") + id"; 

the above code fails to pass id value to CreatePerson function.

like image 569
user1120572 Avatar asked Oct 22 '12 18:10

user1120572


People also ask

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 I pass parameters to URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

What can you use to pass parameters to actions?

You can pass them via a URL, a query string, a request header, a request body, or even a form.


1 Answers

you can pass it this way :

Url.Action("CreatePerson", "Person", new RouteValueDictionary(new { id = id })); 

OR can also pass this way

Url.Action("CreatePerson", "Person", new { id = id }); 
like image 87
HatSoft Avatar answered Sep 22 '22 03:09

HatSoft