Reputation: 584
I have a controller where there are two action methods with same name with HttpPost and HttpGet as shown below , when link with @Url.Action("DoSomething","Controller")
is called then both the method are invoked , how do I call only GET method?
Is there anyway to pass type GET or POST in @URL.Action
or any other way?
[HttpGet]
public ActionResult DoSomething(int ? MyParam)
{
}
[HttpPost]
public ActionResult DoSomething(MyModel Model)
{
}
In Razor View When I build a link like
<a href="@Url.Action("DoSomething","Home",new { MyParam= Whatever})">JustDoIt</a>
Then it calls both POST and GET methods, even after specifiying parameter as int and not the model
Upvotes: 0
Views: 10474
Reputation: 584
Issue was I was having one more ajax request to check if the page has been loaded completely to show progress bar, when removed that portion of the code from layout, It has been working fine without calling any action method twice.
Upvotes: 1
Reputation: 2940
I think that you maybe getting Url.Action()
confused with Html.Action()
(apologies if I'm wrong). As someone mentioned in the comments, Url.Action()
will just render a URL based on the parameters which is intended for say building anchor tags. Html.Action()
on the other hand will invoke an action when building the page for the response at server side which I'm guessing is what your referring too.
If that's the case, then you will need to specify the Action's parameters for DoSomething()
by specifying the route values like so:
@Html.Action("DoSomething", "Home", new {MyParam = 2} )
The result of DoSomething(int MyParam)
would be injected into the page. Html.Action
is not intended to call a POST
so it's not possible to use this on an Action which has been decorated with the POST
verb.
Upvotes: 1
Reputation: 31077
Try adding post in the form declaration:
@using (Html.BeginForm("DoSomething","Controller", FormMethod.Post))
{
}
or using HTML:
<form action="@Url.Action("DoSomething","Controller")" method="POST">
...
</form>
The HTTP verb POST/GET will route your request to the appropriate action. The default is GET.
Upvotes: 2