Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# @Action未在控制器内调用该操作_C#_Asp.net Mvc_Asp.net Mvc 4_Razor - Fatal编程技术网

C# @Action未在控制器内调用该操作

C# @Action未在控制器内调用该操作,c#,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,我是MVC的新手 在Razor中,我需要在控制器中调用一个方法,但它对我不起作用 我的Razor代码如下: @helper SubmitCommnet() { @Html.Action("Post2", "PostShow", new { id = 1111 }); } 我的控制器如下所示: public class PostShowController : SurfaceController { public ActionResult Index() {

我是MVC的新手

在Razor中,我需要在控制器中调用一个方法,但它对我不起作用

我的Razor代码如下:

@helper SubmitCommnet()
{
    @Html.Action("Post2", "PostShow", new { id = 1111 });

}
我的控制器如下所示:

public class PostShowController : SurfaceController
{      
    public ActionResult Index()
    {
        return null;
    }

    public ActionResult Post2(int id)
    {
        return null;
    }
}

你知道为什么它不调用Post2方法吗?

对于
Razor
,你必须使用
Html.ActionLink
来执行此操作,它从Html生成超链接标记,或者直接使用超链接标记,例如:

@Html.ActionLink("Post", // <-- Text for UI 
                "Post2",   // <-- ActionMethod
                "PostShow",  // <-- Controller Name.
                new { id = 1111 }, // <-- Route arguments.
                null  // <-- htmlArguments .. which are none. You need this value
                      //     otherwise you call the WRONG method ...
                      //     (refer to comments, below).
                )

@Html.ActionLink(“Post”,//使用
Razor
,您必须使用
Html.ActionLink
来执行此操作,它从Html生成超链接标记,或直接使用超链接标记,例如:

@Html.ActionLink("Post", // <-- Text for UI 
                "Post2",   // <-- ActionMethod
                "PostShow",  // <-- Controller Name.
                new { id = 1111 }, // <-- Route arguments.
                null  // <-- htmlArguments .. which are none. You need this value
                      //     otherwise you call the WRONG method ...
                      //     (refer to comments, below).
                )

@Html.ActionLink(“Post”,//您使用的是
Html.ActionLink
,就像您使用
Url.Action
一样。请记住,您也需要指定链接文本。有消息告诉我您需要以下方法:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    Object routeValues,
    Object htmlAttributes
)

然后,您可以使用以下命令调用它:

@Html.ActionLink("Click Me", "Post2", "PostShow", new { id = 1111 }, null)

不幸的是,如果您需要提供action、controller和route参数,则需要带有
htmlAttributes
的原型(但是,您可以简单地传递
null
).

您使用的是
Html.ActionLink
,就像使用
Url.Action
一样。请记住,您还需要指定链接文本。有些信息告诉我您需要以下方法:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    Object routeValues,
    Object htmlAttributes
)

然后,您可以使用以下命令调用它:

@Html.ActionLink("Click Me", "Post2", "PostShow", new { id = 1111 }, null)

不幸的是,如果您需要提供操作、控制器和路由参数,那么您需要具有
htmlAttributes
(但是,您可以简单地传递
null
)的原型。

您正在创建一个帮助器,这可能是一个愚蠢的问题,但您是否在视图中使用帮助器?
@helper SubmitCommnet()
?按照其他人所说的,如果你只是想重定向,你可以删除@helper部分,然后链接应该对你有用。显示更多代码,从哪里调用SubmitComment?你正在创建一个helper,这可能是一个愚蠢的问题,但你在视图中使用helper吗?
@helper Submit有什么用通信网()
?按照其他人所说的,如果您只是尝试重定向,您可以删除@helper部分,该链接应该对您有用。显示更多代码,从何处调用SubmitComment?您将Action与ActionLink.Html混淆。Action不呈现链接,它调用控制器操作并返回部分内容。@AaronLS:Typod和修复它就像你评论的一样。很明显我今天在睡觉。(很好的捕捉,泰。)你把Action和ActionLink.Html混淆了。Action不呈现链接,它调用一个控制器动作并返回一个部分。@AaronLS:Typod和修复它就像你评论的一样。很明显我今天在睡觉。(很好的捕捉,泰。)