Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 224
Vinit Patel Avatar asked Jun 12 '14 07:06

Vinit Patel


People also ask

How do I pass multiple parameters to Web API controller methods?

You can pass parameters to Web API controller methods using either the [FromBody] or the [FromUri] attributes. Note that the [FromBody] attribute can be used only once in the parameter list of a method.

Can one action method have multiple views?

Yes, it's possible. Just make sure your views have same view model. From a technical stand point, views don't have to have the same view model implemented within the same action. There's just as long as the controller passes in the expected type.


3 Answers

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}));
like image 77
d219 Avatar answered Oct 22 '22 09:10

d219


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

like image 30
rob waminal Avatar answered Oct 22 '22 09:10

rob waminal


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.

like image 6
LuisEduardoSP Avatar answered Oct 22 '22 09:10

LuisEduardoSP