How to Pass Parameter from @Url.Action to Controller Function

How to pass parameter in @url.Action

@Sribin, below is fundamental which you can use,

your controller code will be,

 // GET: home
public ActionResult Index()
User uObject = new User();
uObject.FirstName = "abc";
return View(uObject);

public ActionResult IndexTest(string id)
return View();

your cshtml code will be

@model WebApplication2.Models.User
ViewBag.Title = "Index";

<br />
<input type="button" onclick="SaveLookup()" value="submit" />

<button type="button" class="btn btn-inverse btn-danger"
onclick="@("window.location.href='" + @Url.Action("IndexTest", "Home", new {id = Model.FirstName}) +"'");">

Instead of using model value you can grap value from view bag or any other tempdata too!!

how to pass parameter with @url.action to controller

If is was an ActionLink you would do this:

@Html.ActionLink("View1", "Task", new {id=0}, null);

So in your case, using Url.Action() it would be:

href="@Url.Action("View1", "Task", new {id=0})"

Which in your sample is:

<a class="v-patient pointer" data-id="0" href="@Url.Action("View1", "Task", new {id=0})">View Patient</a></td>

Url.Action parameters?

The following is the correct overload (in your example you are missing a closing } to the routeValues anonymous object so your code will throw an exception):

<a href="<%: Url.Action("GetByList", "Listing", new { name = "John", contact = "calgary, vancouver" }) %>">

Assuming you are using the default routes this should generate the following markup:

<a href="/Listing/GetByList?name=John&contact=calgary%2C%20vancouver">

which will successfully invoke the GetByList controller action passing the two parameters:

public ActionResult GetByList(string name, string contact) 

Pass Parameter To Controller Using URL.ACTION

Make sure your query string name matches with your action method parameter name

var url = '@Url.Action("HoursByDay", "DashBoard", 
new { dateCompleted= "01/11/2013" })';

If you want to pass a javascript variable value as the value for dateCompleted param, You should first build the url to the action method (without any parameters) and append the querystring value in javascript.

var myDate = $(#yourInputFieldIdForDate").val();
var url = '@Url.Action("HoursByDay", "DashBoard")?dateCompleted=' + myDate;

Replace the jQuery selector to get the date value to suit to your requirements.

Passing url parameters from Url.Action variables in javascript

You usage of Url.Action() in the script is generating & instead of & in the url, so instead of


you need to generate


You can do this by wrapping it in Html.Raw() so its not encoded

addCandidateTaskUrl: "@Html.Raw(Url.Action("Action", "Controller", new { candidacyId = "cId", candidateId = "cndId",  typeOfAction = "tpA" }))"

However, you can make this simpler and more efficient (without the need for multiple .replace()) by just generating the base url, and the appending the query string parameters using the $.param method

view.params = {
baseUrl: '@Url.Action("Action", "Controller")' // Html.Raw not required


var queryString = $.param({ param1Id: actualParam1Id, param2Id: actualParam2Id, ... });
var uri = params.baseUrl + '?' + queryString;

Related Topics

Leave a reply