【问题标题】:MVC5 Actionlink: Pass data as query stringMVC5 Actionlink:将数据作为查询字符串传递
【发布时间】:2014-04-07 22:19:14
【问题描述】:

以下是我的 Html.Actionlink

@Html.ActionLink("Change Team", "Edit", "UserPlayers", new { MatchId = ViewBag.MatchId, UserId = ViewBag.UserId })

当我运行应用程序时,我得到了这个

http://localhost:50013/UserPlayers/Edit?Length=11

作为链接。

我不知道“Length=11”是从哪里来的。

【问题讨论】:

    标签: asp.net-mvc-4


    【解决方案1】:

    您需要添加null 作为最后一个参数:

    @Html.ActionLink("Change Team", "Edit", "UserPlayers", new { MatchId = ViewBag.MatchId, UserId = ViewBag.UserId }, null)
    

    没有这个,你对Html.ActionLink()使用了错误的方法重载

    【讨论】:

    • 为什么会这样呢?这是错误的重载,OP 正在向链接添加 HTML 属性,但为什么会导致它在 url 中附加一个 Length 参数?
    • @JMK 因为没有空值,它很可能会遇到动作链接的重载 - public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, Object routeValues, Object htmlAttributes) 并试图将控制器名称转换为路由值(注意 userplayers 是 11 个字符长) .由于它不是字典或列表,它会得到各种混淆并输出?Length = 11。 MSDN 供参考 - msdn.microsoft.com/en-us/library/dd492124%28v=vs.118%29.aspx
    • 这个答案更详细地解释了原因stackoverflow.com/a/824318/2082842
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 2016-08-27
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多