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

C#asp.net Mvc

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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 });

Solution 2 - C#

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));

Solution 3 - C#

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})';

Solution 4 - C#

Try this:

public ActionResult CreatePerson(int id) //controller 

window.location.href = "@Url.Action("CreatePerson", "Person")?Id='+Id+'";

it's working fine passing parameter.

Solution 5 - C#

This way to pass value from Controller to View:

ViewData["ID"] = _obj.ID;

Here is the way to pass 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"] })'" />

Solution 6 - C#

Need To Two Or More Parameters Passing Throw view To Controller Use This Syntax... Try.. It.

var id=0,Num=254;var str='Sample';    
var Url = '@Url.Action("ViewNameAtController", "Controller", new RouteValueDictionary(new { id= "id", Num= "Num", Str= "str" }))'.replace("id", encodeURIComponent(id));
    Url = Url.replace("Num", encodeURIComponent(Num));
    Url = Url.replace("Str", encodeURIComponent(str));
    Url = Url.replace(/&amp;/g, "&");
window.location.href = Url;

Solution 7 - C#

Try this

public ActionResult CreatePerson(string Enc)

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

you will get the id inside the Enc string.

Solution 8 - C#

should you pass it in this way :

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

Solution 9 - C#

@Url.Action("Survey", "Applications", new { applicationid = @Model.ApplicationID }, protocol: Request.Url.Scheme)

Solution 10 - C#

If you are using Url.Action inside JavaScript then 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!");
  }
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1120572View Question on Stackoverflow
Solution 1 - C#HatSoftView Answer on Stackoverflow
Solution 2 - C#Pàldi GergÅ‘View Answer on Stackoverflow
Solution 3 - C#JainView Answer on Stackoverflow
Solution 4 - C#Benet ShibinView Answer on Stackoverflow
Solution 5 - C#Anjan KantView Answer on Stackoverflow
Solution 6 - C#GuptaView Answer on Stackoverflow
Solution 7 - C#V.VView Answer on Stackoverflow
Solution 8 - C#Zeyad QuneesView Answer on Stackoverflow
Solution 9 - C#Sanjay SharmaView Answer on Stackoverflow
Solution 10 - C#KeerthiView Answer on Stackoverflow