Wednesday, September 24, 2014

Url.Action

...is a pretty good way in Razor markup to get a route to a specified Controller Action. Behold:

<button onclick="@("window.location.href='" + @Url.Action("Bar",
      new { id = @Model.Id }) + "'");">Go</button>

 
 

Unlike @using(Html.BeginForm implementations where one has to specify a Controller and not just an Action, with Url.Action one just needs an Action. The most immediate Controller will be used in the absense of a specification, but you may also give a specification like so:

<button onclick="@("window.location.href='" + @Url.Action("Bar", "Foo",
      new { id = @Model.Id }) + "'");">Go</button>

 
 

This stuff works without regard to the complexity of whether or not you are running an IIS application wherein the URL starts with http://localhost/whatever and the routes will all start with a forward slash specifying the root of the site, and I do indeed mean the real root of the site even in the circumstance of an IIS application.

No comments:

Post a Comment