How to pass parameter from @ Url.Action to controller function in asp.net mvc3

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

Below is the link code:

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

the above code does not pass the id value to the CreatePerson function.

+46
c # asp.net-mvc
Oct 22 '12 at 18:31
source share
8 answers

you can pass it like this:

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

OR can also go this way

 Url.Action("CreatePerson", "Person", new { id = id }); 
+83
Oct 22 '12 at 18:33
source share
 public ActionResult CreatePerson (string id) window.location.href = '@Url.Action("CreatePerson", "Person" , new {id = "ID"})'.replace("ID",id); public ActionResult CreatePerson (int id) window.location.href = '@Url.Action("CreatePerson", "Person" , new {id = "ID"})'.replace("ID", parseInt(id)); 
+11
Oct 27 '15 at 15:04
source share

This way to transfer the value from the controller to view:

 ViewData["ID"] = _obj.ID; 

Here's how to pass a value from View to Controller back:

 <input type="button" title="Next" value="Next Step" onclick="location.href='@Url.Action("CreatePerson", "Person", new { ID = ViewData["ID"] })'" /> 
+2
Aug 12 '15 at 7:53 on
source share

it should be transmitted as follows:

 public ActionResult CreatePerson(int id) //controller window.location.href = "@Url.Action("CreatePerson", "Person",new { @id = 1}); 
0
Apr 6 '15 at 18:35
source share
 @Url.Action("Survey", "Applications", new { applicationid = @Model.ApplicationID }, protocol: Request.Url.Scheme) 
0
06 Sep '16 at 12:21
source share

If you use Url.Action inside JavaScript, you can

 var personId="someId"; $.ajax({ type: 'POST', url: '@Url.Action("CreatePerson", "Person")', dataType: 'html', data: ({ //insert your parameters to pass to controller id: personId }), success: function() { alert("Successfully posted!"); } }); 
0
Aug 14 '17 at 5:41 on
source share

try it

public ActionResult CreatePerson (line Enc)

window.location = '@ Url.Action ("CreatePerson", "Person", new {Enc = "id", actionType = "Disable"})'. replace ("id", id) .replace ("&", "&");

you will get the identifier inside the line Enc.

0
Dec 19 '17 at 5:04 on
source share
 public ActionResult CreatePerson(int id) //controller window.location.href = '@Url.Action("CreatePerson", "Person")?id=' + id; 

or

 var id = 'some value'; window.location.href = '@Url.Action("CreatePerson", "Person", new {id = id})'; 
-2
Dec 16 '16 at 15:43
source share



All Articles