.NET MVC Call method on different controller

前端 未结 7 1395
Happy的楠姐
Happy的楠姐 2020-11-30 06:26

Can anybody tell me how to call a method on a different controller from within an action method? I don\'t want to redirect. I want to call a method on a different controller

相关标签:
7条回答
  • 2020-11-30 06:50

    Could you just instantiate the controller in your action method and call the other method you need?

    public ActionResult YourActionMethod()
    {
       SomeController c = new SomeController();
       ActionResult result = c.SomeMethod();
    
       return View();
    }
    
    0 讨论(0)
  • 2020-11-30 06:52

    You can use the following approach to invoke a method on the other controller:

    var otherController = DependencyResolver.Current.GetService<OtherController>();
    var result = otherController.SomeMethod();
    

    This worked for me in ASP.NET MVC5. Hope that it will work for you as well.

    0 讨论(0)
  • 2020-11-30 06:59

    I was looking for the same thing, but seriously people, why the need to write such complicated answers.

    Here's a post that will answer it very simply: Using Html.ActionLink to call action on different controller

    Basically you just have to use this overload of the actionlink: ActionLink(HtmlHelper, String, String, String, Object, Object)

    So you will have: ActionLink("linkText", "actionName", "controllerName", routeValues, htmlAttributes)

    If you don't have any routeValues (which are the Inputs for the Action) or htmlAttributes, you have to set them as null.

    Here's an example call: @Html.ActionLink("Add New Student", "Create", "Student", null, new { @class = "btn btn-primary" })

    0 讨论(0)
  • 2020-11-30 07:02

    Sounds in my ears like you should refactor your application, and extract the functionality that generates the string out to a new seperate class (or reuse an existing class, if you have one that fits) and let both controllers use that class.

    0 讨论(0)
  • 2020-11-30 07:02

    Looks like you're trying to do something the controllers aren't designed for. Design your required method as an public method in some class and invoke from both controller actions.

    0 讨论(0)
  • 2020-11-30 07:08

    You can achieve this via the Action method of HtmlHelper.

    In a view, you would do it like this:

    @Html.Action("OtherAction")
    

    However it's not straightforward to obtain an instance of HtmlHelper in an action method (by design). In fact it's such a horrible hack that I am reluctant to even post it...

    var htmlHelper = new HtmlHelper(new ViewContext(
                                          ControllerContext, 
                                          new WebFormView(ControllerContext, "HACK"),
                                          new ViewDataDictionary(),
                                          new TempDataDictionary(),
                                          new StringWriter()),
                                    new ViewPage());
    
    var otherViewHtml = htmlHelper.Action("ActionName", "ControllerName");
    

    This works on MVC 3. You might need to remove the StringWriter arg from the ViewContext constructor for MVC 2, IIRC.

    </hack>

    0 讨论(0)
提交回复
热议问题