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)
Share:
10,699
Jude
Author by

Jude

.NET Developer SOreadytohelp

Updated on June 04, 2022

Comments

  • Jude
    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
    Jude over 10 years
    It 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
    Jude over 10 years
    This 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.
    Cj S. over 10 years
    If 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))