如何在Url.Action中发送多个参数?

Vin*_*tel 14 asp.net-mvc c#-5.0 asp.net-mvc-3

你如何发送多个参数Url.Action

我有一个带动作的控制器,我想要2个参数,但没有收到第2个参​​数.

我的代码是:

@Url.Action("Products", "Jquery", new { categoryid = 1, Productid = 2})

Publc Action Jquery(int categoryid ,int Productid)
{

}
Run Code Online (Sandbox Code Playgroud)

但我只收到categoryid,每次Productid都是空的.

请建议我做什么?

d21*_*219 16

你是从JQuery调用这个动作的吗?听起来你可能出现这些症状(至少可能是@roberto); 若是这样包裹它Html.Raw:

@Html.Raw(@Url.Action("Jquery", "Products", new { @categoryid = 1, @Productid = 2}));
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用。我不必为变量名称添加`@` (2认同)

rob*_*nal 10

试试这样.

@Url.Action("Jquery", "Products", new { @categoryid = 1, @Productid = 2})

public ActionResult Jquery(int categoryid, int Productid)
{
    return View();
}
Run Code Online (Sandbox Code Playgroud)

你应该在你的行动中获得2个参数.假设Jquery行动在ProductController

  • 我有同样的问题,这对我不起作用,实际上,问题是它构建了添加如下特殊字符的URL:/ Controller/Action?param1 = foo& param2 = fooo (4认同)
  • 我试过这个,但它不起作用,无法接收第二段。它总是空的 (2认同)
  • 实际上,如果您在参数前添加`@` 前缀并不重要。它仍然适用于第一个。我不知道如何解释它。也许你有一些拼写错误? (2认同)

Lui*_*oSP 5

我遇到了同样的问题,添加了@,但没有用。

对我有用的是@Html.Raw指令。

@Html.Raw(@Url.Action("Jquery", "Products", new { categoryid = 1, Productid = 2}))

public ActionResult Jquery(int categoryid, int Productid)
{
    return View();
}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您可以获取控制器中的所有参数,而不仅仅是第一个。