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.

8
  • Do you mean you want to call it from JavaScript? or Hardcode the Link in your HTML file? Commented Mar 22, 2016 at 9: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. Commented Mar 22, 2016 at 9:09
  • Possible duplicate of Calling ASP.NET MVC Action Methods from JavaScript Commented Mar 22, 2016 at 9:09
  • I added some infos to be more clear sorry Commented Mar 22, 2016 at 9: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. Commented Mar 22, 2016 at 9:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this is what I was looking for ! I think I already tried to use it but now it's magically working thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.