asp.net-mvc 如何使用 Html.Action?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8874775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:41:32  来源:igfitidea点击:

How can I use Html.Action?

asp.net-mvcasp.net-mvc-3

提问by Samantha J T Star

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

有人可以解释我如何使用 Html.Action 做到这一点

回答by Nadir Muzaffar

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

您应该查看Action方法的文档;它解释得很好。对于您的情况,这应该有效:

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

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

controllerName参数将默认为从中Html.Action调用的控制器。因此,如果您尝试从另一个控制器调用操作,则必须像这样指定控制器名称:

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

回答by Gustavo F

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.Actionpassing the parameters:

然后,使用Html.Action传递参数:

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

and use in Controller:

并在控制器中使用:

public ActionResult PkRkAction(PkRk pkrk) {
    return PartialView(pkrk);
}

回答by okproject

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

另一种情况是http重定向。如果您的页面将 http 请求重定向到 https,则可能是您的部分视图尝试自行重定向。

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

它再次导致同样的问题。对于这个问题,你可以重新组织你的.net错误页面或iis错误页面配置。

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.

只需确保您将请求重定向到正确的错误或未找到页面,并确保此错误页面包含无问题的部分。如果您的页面仅支持 https,请不要在不使用 https 的情况下将请求转发到错误页面,如果错误页面包含部分,则此部分会尝试从请求的 url 单独重定向,这会导致问题。