ASP.NET MVC Beta Routes, Controller Actions, Parameters and ActionsLinks…putting it all together

I'm having some trouble with ASP.NET MVC Beta, and the idea of making routes, controller actions, parameters on those controller actions and Html.ActionLinks all work together. I have an application that I'm working on where I have a model object called a Plot, and a corresponding PlotController. When a user creates a new Plot object, a URL friendly name gets generated (i.e.). I would then like to generate a "List" of the Plots that belong to the user, each of which would be a link that would navigate the user to a view of the details of that Plot. I want the URL for that link to look something like this: http://myapp.com/plot/my-plot-name. I've attempted to make that happen with the code below, but it doesn't seem to be working, and I can't seem to find any good samples that show how to make all of this work together.

My Route definition:

routes.MapRoute( "PlotByName", "plot/{name}", new { controller = "Plot", action = "ViewDetails" } );

My ControllerAction:

[Authorize]
public ActionResult ViewDetails( string plotName )
{
    ViewData["SelectedPlot"] = from p in CurrentUser.Plots where p.UrlFriendlyName == plotName select p;
    return View();
}

As for the ActionLink, I'm not really sure what that would look like to generate the appropriate URL.

Any assistance would be greatly appreciated.

Answers


The answer is pretty simple: You have to supply enough values in your "ActionLink" that will fulfill your Route. Example:

<%= Html.ActionLink("Click Here", "ViewDetails", "Plot", new { name="my-plot-name" }, null)%>

If you leave out the "name=" part of the ActionLink method, then the RouteEngine won't see this link as being good enough to "match"... so then it would go to the default route.

This code above will make the URL look the way you want it.


Need Your Help

unistd.h is not available, How to use fork()

c fork dos dosbox

I want to exercise some UNIX commands using C language. When I include &lt;unistd.h&gt; header file and when I use fork() method it gives me a compile time error: "can not include unistd.h". I chec...