【问题标题】:Only first parameter value is getting while calling controller method using Url.action.使用 Url.action 调用控制器方法时仅获取第一个参数值。
【发布时间】:2013-01-29 21:13:43
【问题描述】:

我正在使用Url.action 之类的方式调用控制器方法,

location.href = '@Url.Action("Display", "Customer", new { username = "abc",name = "abcdef",country = "India",email = "abc@hmail.com",phone = "9456974545"})';

我的控制器方法是,

public void Display(string username, string name, string country, string email, string phone)
{    }

在这个方法中,我只能得到第一个参数的值(username)。它没有得到传递的其他参数值。所有其他值为空。

请给我建议,有什么问题?

【问题讨论】:

  • 点击ActionLink时浏览器地址栏能看到查询字符串参数吗?
  • 是的。我可以在浏览器地址栏中看到查询字符串参数。网址是这样的:localhost:60710/Customer/…
  • 这是什么?粘贴到这里
  • 嗯.. 像这样试试@Html.Raw(@Url.Action("Display","Customer", new { username = "abc",name = "abcdef"}))

标签: asp.net-mvc controller url.action


【解决方案1】:

默认情况下,使用 @ 块发出的每个内容(不是 IHtmlString)都会被 Razor 自动 HTML encoded

所以,@Url.Action() 也被编码,你得到的是纯文本。而& 编码为&

如果你不想编码,那么你应该使用@Html.Raw(@Url.Action("",""))

你的问题的答案是:

location.href = '@Html.Raw(@Url.Action("Display", "Customer", new { username = "abc",name = "abcdef",country = "India",email = "abc@hmail.com",phone = "9456974545"}))';

希望对你有帮助

【讨论】:

    【解决方案2】:

    '&' 被编码为 '&' 时出现问题

    模型绑定器无法识别此值。您需要通过使用 Html.Raw 函数呈现链接来防止这种编码。

    使用'@Html.Raw(Url.Action......)'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      相关资源
      最近更新 更多