未找到操作方法——即使它在控制器中

Action Method Not Found -- Even Though It IS In The Controller

阅读文档后我的假设是我们可以使用@Html.Action("ActionName", "ControllerName") 从视图调用控制器中的操作方法。

这是一个错误的假设吗?因为如果我的 logic/assumption 一开始是错误的,了解它会很有帮助。

我的 HomeController.cs

中有以下代码
[AllowAnonymous]
public async Task<ActionResult> DidItWork()
{
        var dash = "dash equals nash";

        var manager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        var user = new ApplicationUser { UserName = "test2 username2", Email = "testemail2@gmail.com" };
        var result = await manager.CreateAsync(user, "passwordGoesHere1!");
        if (result.Succeeded)
        {
            await manager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account");

            return View();
        }

        //AddErrors(result);
        return View();    
}

我的 _Layout.cshtml

中有以下脚本
    @*script for someone who presses the send/email button*@ 
    <script>
        $("#sendButton").click(function () {
            alert("Handler for .click() called.");
            @Html.Action("DidItWork", "Home")
        });
    </script>

在启动程序和调试期间,DidItWork() 似乎在未单击 #sendButton 的情况下被调用——这是为什么?

单步执行时,只要用户名和电子邮件是唯一的,似乎一切正常。但是,最后,一旦完成,它就会转到 @Html.Action("DidItWork", "Home") 行并抛出异常。

Thoughts/suggestions 解决这个问题?异常信息如下:

An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code Additional information: Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'. Inner Exception: {"The view 'DidItWork' or its master was not found or no view engine supports the searched locations. The following locations were searched:..."}

My assumption after reading through the documentation was that we could use @Html.Action("ActionName", "ControllerName") to call an action method in a controller from a view.

Is that a false assumption?

没有

Upon starting up the program, and during debugging, DidItWork() appears to be called without ever clicking the #sendButton -- why is that?

因为这就是 Html.Action 助手的工作方式。它在服务器 上调用操作 并将此操作的结果插入视图。

Thoughts/suggestions for fixing this? The exception info is below:

由于您是在谈论使用 javascript 调用操作,因此您不应使用 Html.Action 等服务器端视图呈现,而应使用客户端脚本。例如,您可以使用 AJAX:

<script>
    $("#sendButton").click(function () {
        $.ajax({
            url: '@Url.Action("DitItWork", "Home")',
            success: function(reuslt) {
                // result will contain the execution of the DidItWork action.
                // Right now it is an error, because you don't seem to have defined
                // a DidItWork.cshtml page. Once you fix this it will work
                // as expected
                // Here you probably want to insert the result somewhere
                // in your markup
            }
        });
        // return false to cancel the default action and leave the AJAX call
        return false;
    });
</script>

这段代码段

@Html.Action("DidItWork", "Home")

调用 DidItWork 操作和您的代码 returning View()。在构建过程中,您实际上需要打开 Home/DidItWork.cshtml 因此您必须将 return 语句更改为现有视图。