How do you send multiple parameters in a Url.Actio

2020-06-01 10:59发布

问题:

How do you send multiple parameters in an Url.Action?

I have a controller with an action, and I want 2 parameters, but the 2nd parameter is not being received.

My code is:

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

Publc Action Jquery(int categoryid ,int Productid)
{

}

but I only receive categoryid, every time Productid is null.

Please suggest to me what to do?

回答1:

try it like this.

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

public ActionResult Jquery(int categoryid, int Productid)
{
    return View();
}

you should get the 2 parameters in your action. Assuming the Jquery Action is under ProductController



回答2:

Were you calling this action from JQuery? Sounds like you could be from the symptoms (or at least @roberto could be); if so wrap it in Html.Raw:

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


回答3:

I had the same problem, added the @'s and didn't work.

What worked for me was the @Html.Raw instruction.

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

public ActionResult Jquery(int categoryid, int Productid)
{
    return View();
}

In this way you can get all parameters in the controller, not only the first.