简体   繁体   English

Controller 动作方法未被调用在 ASP.NET Core 中发出发布请求

[英]Controller action method not being called making a post request in ASP.NET Core

I am trying to use model binding to send a file selected by the user to the controller, this action method is not being hit and I am not sure why.我正在尝试使用 model 绑定将用户选择的文件发送到 controller,此操作方法未被命中,我不确定为什么。 I put a breakpoint inside but it will not get hit.我在里面放了一个断点,但它不会被击中。

Here is my import view:这是我的导入视图:

@model FileModel

<h1 class="title-underline">Import file</h1>

<div class="form-group">
    <form asp-controller="Import" asp-action="FileImport" method="POST" enctype="multipart/form-data">
        <label for="importFile">Select a past grant to import</label>
        <input type="file" id="importFile" name="importFile" asp-for="ImportFile">
    </form>
    <button type="submit" id="btnSubmit" class="btn btn-tnl btn-block">Import</button>
</div>

Here is my model:这是我的 model:

public class FileModel
{
    public IFormFile ImportFile { set; get; }
}

Here is my action method inside my ImportController:这是我的 ImportController 中的操作方法:

    [HttpPost, ValidateAntiForgeryToken]
    public IActionResult FileImport(FileModel model)
    {
        IFormFile file = model.ImportFile;

        var fileName = Path.GetFileName(file.FileName);
        var contentType = file.ContentType;



        return View("Index");

    }

I have provided the controller, action, method, and enctype so I'm not sure what else is missing which is stopping the method from being hit.我已经提供了 controller、操作、方法和加密类型,所以我不确定还缺少什么阻止方法被命中。 Is it something to do with the model in the parameter?是不是和参数中的model有关?

One problem that I see is the submit button is outside of the form.我看到的一个问题是提交按钮在表单之外。 It is not doing anything when you click it.当您单击它时,它没有执行任何操作。 Try bringing it into the <form>...</form> block and make the input type = "submit", not "button".尝试将其放入<form>...</form>块并使输入类型 =“提交”,而不是“按钮”。 Or write a click handler for it that would call submit on the form.或者为它编写一个点击处理程序来调用表单上的提交。

You can check the syntax here https://www.w3schools.com/html/html_form_elements.asp您可以在此处查看语法https://www.w3schools.com/html/html_form_elements.asp

<div class="form-group">
    <form asp-controller="Import" asp-action="FileImport" method="POST" enctype="multipart/form-data">
        <label for="importFile">Select a past grant to import</label>
        <input type="file" id="importFile" name="importFile" asp-for="ImportFile">
        <input type="submit" id="btnSubmit" class="btn btn-tnl btn-block" value="Import">
    </form>
</div>

Your button should be inside form tag.您的按钮应该在表单标签内。 Not outside.不在外面。

<form>
   <button type="submit">Import</button>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.NET Core Web API中使用依赖项注入时未调用Controller Action方法 - Controller Action method not called while using dependency injection in asp.net core web api "ASP.NET Web App CORE:未调用控制器中的 HttpPost 编辑方法" - ASP.NET Web App CORE: the HttpPost Edit Method in Controller not being called ASP.Net核心有时会动作控制器被调用两次 - ASP.Net core sometimes the action controller is called twice ASP.NET 内核 HTTP 错误 405 - 路由到 controller 操作后的错误方法 - ASP.NET Core HTTP Error 405 - Bad Method when routing to post controller action ASP.NET Core 3.1 SignalR:方法未被调用 - ASP.NET Core 3.1 SignalR: method not being called AJAX POST 请求未将数据传递给 ASP.NET Core 2.2 中的控制器方法 - AJAX POST Request Not Passing Data to Controller Method in ASP.NET Core 2.2 在asp.net核心中发送带有防伪令牌的post ajax请求不访问控制器方法 - Send post ajax request with antiforgery token in asp.net core do not access controller method 使用默认接口方法作为 controller 动作 ASP.NET 核心 - Use default interface method as controller action ASP.NET Core 向ASP.NET MVC控制器中的操作方法发出HttpPost请求 - Make HttpPost request to an action method in an ASP.NET MVC controller ASP.NET 核心 AuthorizationHandler 未被调用 - ASP.NET Core AuthorizationHandler not being called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM