MVC Quick Tips

1. How to redirect to another action from with in an Action
return
RedirectToAction("ToActionName", new { id = 99 });

id will be name of parameter for which value is being passed.

Advertisements

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().

#mvc, #razor