简体   繁体   English

RouteLink 空 Href

[英]RouteLink empty Href

Im using MVC3 with the Razor View Engine.我将 MVC3 与 Razor 视图引擎一起使用。 I want to use a certain Route for a Href我想为 Href 使用某个 Route

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.但 @Html.RouteLink 只是不起作用。 it returns an empty href for the link它为链接返回一个空的href

Here are some of my attempts这是我的一些尝试

@Html.RouteLink("Add New Item", "AddItem", new { controller="List", action="AddItem", listAreaName = Model.ListAreaName, listSlug = Model.Slug })<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.我找到的大部分材料都是用于 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:在最后一个路由链接中你有一个错误,你不应该使用Controller后缀:

@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.就其他人而言,它们都生成以下 url: /ListAreaName/Slug/Add in ASP.NET MVC 3 RTM 假设您已设置默认路由并且您已添加问题中显示的路由。

Edit编辑

Only one element in your route can have a default omitted.您的路线中只有一个元素可以省略默认值。 You don't have defaults for either listAreaName or listSlug.您没有 listAreaName 或 listSlug 的默认值。 See this question: asp mvc routing with two optional parameters看到这个问题: 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.您的路线实际上并不包括 controller 和操作参数。 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.我要做的另一件事是确保 model 实际上包含ListAreaNameSlug值。

@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.此外,您真的应该使用 CSS 将这些样式设置为块级元素,而不是尽可能在其后添加<br/>

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM