Welcome to ASP.NET Guild

Be sure to come back often and tell others. If you have any tips, tricks, examples, please email them to me at chris.williams@techguilds.com and I will post them. Check out our ASP.NET QuickStart and C# QuckStart Libraries. Below is my latest articles.

Sunday, February 28, 2016

MVC RedirectToAction Explained for Beginners

RedirectToAction should be simple but sometimes they are confusing to beginners.  There are 3 ways to call RedirectToAction
  1. If you are already on a page within a controller you can use the shortest one which is only the caption and the action.

    eg. RedirectToAction("MyActionName")
  2. If you are referencing another controller or you want to include parameters.

    IMPORTANT: It may seem redundant but you need to list the action name twice. Notice Login is listed twice. 

    eg. RedirectToAction("Login", new RouteValueDictionary(new { controller = "Account", action = "Login", returnUrl = "/PersonalizedLearn" }));
I hope this clears up how RedirectToAction calls work.

If you have any questions feel free to email me at chris.williams@readwatchcreate.com



MVC Html.ActionLinks Explained for Beginners

Html.ActionLink should be simple but sometimes they are confusing to beginners.  There are 3 ways to call @Html.ActionLink:


  1. If you are already on a page within a controller you can use the shortest one which is only the caption and the action.

    eg. @Html.ActionLink("Click Here", "MyActionName")
  2. If you are referencing another controller or you want to include attributes on the link such as the css class

    eg.

    @Html.ActionLink("Click Here", "Index", "Home", null)

    @Html.ActionLink("Click Here", "Index", "Home", new { @class = "myHrefClass" })
  3. If you need to pass parameters to your action then you need to use the full version.

    IMPORTANT: If you are NOT Specifying a class, you still need to pass NULL or it will assume that the parameters you are passing are actually the attributes like in the version in the section way above.

    eg.

    @Html.ActionLink("Click Here", "Index","Home", new { paramName = value, param2Name = value}, null)

    @Html.ActionLink("Click Here", "Index","Home", new { paramName = value, param2Name = value}, new { @class = "myHrefClass" })


I hope this clears up how ActionLink calls work.  If you have any questions feel free to email me at chris.williams@readwatchcreate.com



Are you a .NET Developer or Contractor interested in working with Sitecore or Dynamics CRM?

Apply for our Mentorship Program. If accepted, we will mentor you on Sitecore and provide you with project to help you build your skills and make some money at the same time. If you are interested send your resume with details on why you want to work with Sitecore or Dynamics CRM to: Chris Williams - chris.williams@techguilds.com or Dennis Augustine - dennis.augustine@techguilds.com We look forward to working with you to achieve your goals.