Jay*_*Sun 0 razor asp.net-mvc-3
我有一个看起来像的控制器方法
public ViewResult Index(string id, string participant="", string flagged = "")
id是我的global.asax中此控制器的路由值.我想将其他值作为常规参数传递,因此链接看起来像
.../controller/Index/id?participant=yes&flagged=no
有没有办法在MVC 3中使用Razor脚本生成这样的链接?
您可以传递routeValuesActionLink方法的参数中的所有参数:
@Html.ActionLink(
    "go to index",                  // linkText
    "index",                        // actionName
    new {                           // routeValues
        id = "123", 
        participant = "yes", 
        flagged = "no" 
    }
)
假设默认路由设置,这将生成:
<a href="/Home/index/123?participant=yes&flagged=yes">go to index</a>
更新:
要进一步详细说明您发布的注释,如果ActionLink生成了一个url,Length=6例如这意味着您使用了错误的重载.例如,这是错误的:
@Html.ActionLink(
    "go to index",             // linkText
    "index",                   // actionName
    "home",                    // routeValues
    new {                      // htmlAttributes
        id = "123", 
        participant = "yes", 
        flagged = "no" 
    }
)
很明显,为什么这个错误来自我在每个参数名称中添加的注释.因此,请确保您仔细阅读Intellisense(如果您足够幸运,让智能感知器在Razor中工作:-))来选择正确的辅助方法重载.
要指定控制器名称的情况下的正确重载如下:
@Html.ActionLink(
    "go to index",             // linkText
    "index",                   // actionName
    "home",                    // controllerName
    new {                      // routeValues
        id = "123", 
        participant = "yes", 
        flagged = "no" 
    },
    null                       // htmlAttributes
)
请注意,null它作为最后一个参数传递.这是与htmlAttributes参数对应的内容.
| 归档时间: | 
 | 
| 查看次数: | 2871 次 | 
| 最近记录: |