开发者

RouteLink empty Href

Im using MVC3 with the Razor View Engine. I want to use a certain Route for a Href

the following is the Route registration, (the route seems to work as i can use the browser to directly use it)

routes.MapRoute(
    "AddItem",
    "{listAreaName}/{listSlug}/Add",
    new { controller = "List", action = "AddItem" }
    );

but the @Html.RouteLink just does not work. it returns an empty href for the link

Here are some of my attempts

@Html.RouteLink("Add New Item", "AddItem", new { controller="List", action="AddItem", listAreaName = Model.ListAreaName, listSlug = Model.Slug }开发者_JAVA百科)<br />
@Html.RouteLink("Add New Item1", "AddItem", new { controller = "List", listAreaName = Model.ListAreaName, listSlug = Model.Slug })<br />
@Html.RouteLink("Add New Item2", "AddItem", new { action = "AddItem", listAreaName = Model.ListAreaName, listSlug = Model.Slug })<br />
@Html.RouteLink("Add New Item3", "AddItem", new { controller = "list", action = "additem", listAreaName = Model.ListAreaName, listSlug = Model.Slug })<br />
@Html.RouteLink("Add New Item4", "additem", new { controller = "List", action = "AddItem", listAreaName = Model.ListAreaName, listSlug = Model.Slug })<br />

@Html.RouteLink("Add New Item5", "additem", new { listAreaName = Model.ListAreaName, listSlug = Model.Slug })<br />
@Html.RouteLink("Add New Item6", "AddItem", new { controller = "ListController", action = "AddItem", listAreaName = Model.ListAreaName, listSlug = Model.Slug })<br />

if I miss out the route name, it generates working link, but its no in the right format

most of the material i found was for a preview release of v1. has this issue be sorted?

any idea's?


In the last route link you have a mistake, you should not use the Controller suffix:

@Html.RouteLink(
    "Add New Item6", 
    "AddItem", 
    new { 
        controller = "List", 
        action = "AddItem", 
        listAreaName = Model.ListAreaName, 
        listSlug = Model.Slug 
    }
)

As far as the others are concerned they all generate the following url: /ListAreaName/Slug/Add in ASP.NET MVC 3 RTM assuming you have the default routes setup and you have added the route shown in your question.


Edit

Only one element in your route can have a default omitted. You don't have defaults for either listAreaName or listSlug. See this question: asp mvc routing with two optional parameters

routes.MapRoute(
    "AddItem",
    "{listAreaName}/{listSlug}/Add",
    new
    {
        controller = "List",
        action = "AddItem",
        listAreaName = "foo", // <-- add these
        listSlug = UrlParameter.Optional
    });

Original

Your route doesn't actually include the controller and action parameters. Have you tried omitting them from the route values (they will be inferred when the route is decoded on the request). The other thing that I would do is make sure that the model actually includes the ListAreaName and Slug values.

@Html.RouteLink("Add New Item",
                "AddItem",
                new
                {
                     listAreaName = Model.ListAreaName,
                     listSlug = Model.Slug 
                })<br />

Also, you really should be using CSS to style these as block level elements rather than adding a <br/> after it if at all possible.

<style>
   .block { display: block; }
</style>

@Html.RouteLink("Add New Item",
                "AddItem",
                new
                {
                     listAreaName = Model.ListAreaName,
                     listSlug = Model.Slug 
                },
                new { @class = "block" })
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜