0

Is there a way to call a controller without clicking on a link?

By this I mean, without using @Html.ActionLink for example, something automatic that's call after a condition.

Thanks in advance !

Edit :

There is some code :

if (IsPost)
{
    if (!Request["idInterventions"].IsEmpty())
    {
                    string[] AllStrings = Request["idInterventions"].Split(',');
                    List<int> list = new List<int>();
                    foreach (string item in AllStrings)
                    {
                        int value = int.Parse(item);
                        list.Add(value);
                    }
                    Model.toFacture(list);
                    isDone = true;
                    //Need to call a controller method here 
    }
}

So my code is triggered after a POST.

MrPixel6
  • 357
  • 1
  • 4
  • 19
  • Do you mean you want to call it from JavaScript? or Hardcode the Link in your HTML file? – Haitham Shaddad Mar 22 '16 at 09:08
  • 1
    _When_ do you want an HTTP request to be issued? Upon page load? Every N seconds? When the user interacts with some element? Keyword here is JavaScript. – CodeCaster Mar 22 '16 at 09:09
  • Possible duplicate of [Calling ASP.NET MVC Action Methods from JavaScript](http://stackoverflow.com/questions/8952953/calling-asp-net-mvc-action-methods-from-javascript) – CodeCaster Mar 22 '16 at 09:09
  • I added some infos to be more clear sorry – MrPixel6 Mar 22 '16 at 09:13
  • So you want to call another controller method from within a controller method? You do that like you call any other method. But you shouldn't; extract the logic out of the controller into a separate class, and call that class method from both places. – CodeCaster Mar 22 '16 at 09:14
  • No this piece of code is in my view in fact ("maybe not a good idead but I'm new with asp.net,c#,etc...) so I want to know how to call it from my view – MrPixel6 Mar 22 '16 at 09:15
  • The code you've provided appears to be in a Controller - if that code is in the view, then it's probably in the wrong place. Either way, this can be done as previously advised by extracting the "need to call method" into a "utility" class and calling it from both locations. – freedomn-m Mar 22 '16 at 10:01
  • From your view, you can call any controller **action** using @Html.Partial. In your example code, it doesn't look like you're not calling an "action", but just another "method". Your controllers should only have public actions (that return `HtmlResult` or derivatives) and not general methods - these should be elsewhere. – freedomn-m Mar 22 '16 at 10:03

1 Answers1

1

In Razor, you can use

Html.RenderAction("ActionName", "ControllerName", new { Area = "SomeArea", someParameter = Model.SomeParameterValue });

This renders the view returned by the action directly.

In controllers, you can use

return RedirectToAction("ActionName", "ControllerName");

This will result in a HTTP 302 Found redirect.

Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
  • Thank you this is what I was looking for ! I think I already tried to use it but now it's magically working thanks ! – MrPixel6 Mar 22 '16 at 10:26