[英]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.