ActionLink passing parameter as QueryString

In MVC Razor ActionLinke, it is a common requirement to pass parameter as a query string. It creates URL like below:

/Home/Edit/1

and of course basically it is <a href="/CodeTest/EditClass/1">Edit</a>

ActionLink provide different overloads, using one of them such links can be generated:

@Html.ActionLink("Home", "Edit", new {id=1})

Instead of hard coded strign a model property can also be used:

@Html.ActionLink("Home", "Edit", new {id=Model.Id})

Important

If you are using T4MVC templates is a little different from above examples, then you will code as per below example:
@Html.ActionLink("Edit",MVC.CodeTest.EditClass()
.AddRouteValue("id",1))

and using model:
@Html.ActionLink("Edit",MVC.CodeTest.EditClass()
.AddRouteValue("id",itm.ClassID))

For passing multiple parameters use AddRouteValues().

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s