Call an Action Method From View
10,699
Solution 1
Use the ActionLink HtmlHelper like this...
@Html.ActionLink("Call YorumEkle Method", "YorumEkle", new {@id = "hello", @pid="yarg!"}, null))
It will generate the link you need.
Solution 2
I can't comment but building on top of Cj's answer you can also specify which controller the action is on:
@Html.ActionLink("Link Text", "Action Name","Controller", new {@id = "hello", @pid="yarg!"}, null)
In your case
@Html.ActionLink("Link Text", "YorumEkle","CvAramaController", new {@id = "hello", @pid="yarg!"}, null)
Comments
-
Jude almost 2 years
Here is the Action Method
[HttpGet] [ViewException] [UserFilter(OpUserAuthType.Admin, OpUserAuthType.Normal)] public ActionResult YorumEkle(string id, string pid) { ..... }
It has a view. But from another view, I want that action method to be called by an onclick event or anything to be clicked on. For instance:
<a href="" onclick="">Call YorumEkle Method</a>
-
Jude over 10 yearsIt didn't work. Action Method "YorumEkle" in a controller called CvAramaController which is inside the Controller folder. The view I am calling the action method is located under Views/Cikti/Sablon. Do you think that may cause a problem. By the way the controller has the Post Action Method with the same name.
-
Jude over 10 yearsThis is from Global.asax: new { controller = "CvArama", action = "Index", id = UrlParameter.Optional, pid = UrlParameter.Optional }); routes.MapRoute("CvArama", "cv-arama/{action}/{id}/{pid}". Do you think that puts the things in the wrong direction?
-
Cj S. over 10 yearsIf the action is in a different controller, then you'll need to specify that using a different ActionLink overload.
@Html.ActionLink("Call YorumEkle Method", "YorumEkle", "CvArama", new { @id = "hello", @pid = "yarg!" }, null))