42

I am trying to understand how to use:

@Html.Action("GetOptions", )

What I would like to do is to pass a call to my controller and pass the parameters:

pk = "00" and rk = "00"

Can someone explain how I can do that with the Html.Action

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

3 Answers3

43

You should look at the documentation for the Action method; it's explained well. For your case, this should work:

@Html.Action("GetOptions", new { pk="00", rk="00" });

The controllerName parameter will default to the controller from which Html.Action is being invoked. So if you're trying to invoke an action from another controller, you'll have to specify the controller name like so:

@Html.Action("GetOptions", "ControllerName", new { pk="00", rk="00" });
Nadir Muzaffar
  • 4,772
  • 2
  • 32
  • 48
  • Please let me know what is GetOptions? is it an action method? – user786 May 18 '15 at 05:25
  • 1
    Yes, it's an action method name. Documentation I'd linked explains in better detail. – Nadir Muzaffar May 18 '15 at 20:02
  • 4
    "it's explained well"?! Are you kidding?! I think it couldn't be explained more stupid! MS Help says "Invokes the specified child action method using the specified parameters and controller name and returns the result as an HTML string." Actually HtmlHelper.Action() IS a method and is not "invoking" a method. Also what is referred to as "actions" in MVC are not methods, but MVC actions! Such MVC actions are NOT "invoked" by HtmlHelper.Action(), nor is the result of such invocation returned as an Html String. The result of the action method is a link that will call the specified MVC action. – Jens Mander Oct 06 '18 at 04:41
  • I guess the questioner (Samantha J T Star) needs to know how to use or usage of Html.Action otherwise almost everybody knows how to set parameters for this method! Usually MS documents are NOT explained well!! – gyousefi Sep 04 '19 at 17:58
9

first, create a class to hold your parameters:

public class PkRk {
    public int pk { get; set; }
    public int rk { get; set; }
}

then, use the Html.Action passing the parameters:

Html.Action("PkRkAction", new { pkrk = new PkRk { pk=400, rk=500} })

and use in Controller:

public ActionResult PkRkAction(PkRk pkrk) {
    return PartialView(pkrk);
}
Jacob Sobus
  • 961
  • 16
  • 25
Gustavo F
  • 2,071
  • 13
  • 23
0

Another case is http redirection. If your page redirects http requests to https, then may be your partial view tries to redirect by itself.

It causes same problem again. For this problem, you can reorganize your .net error pages or iis error pages configuration.

Just make sure you are redirecting requests to right error or not found page and make sure this error page contains non problematic partial. If your page supports only https, do not forward requests to error page without using https, if error page contains partial, this partials tries to redirect seperately from requested url, it causes problem.

okproject
  • 85
  • 1
  • 2
  • 6