首页 > 解决方案 > 选择文件时不发送表单值

问题描述

如果我在不选择文件的情况下提交表单,我可以看到office参数是通过正确的办公室名称发送的

但是,在从file输入中选择一个文件然后提交后,两个参数都作为null

是什么赋予了?

控制器

[HttpPost]
public IActionResult Upload(string office, string path)
{
    return RedirectToAction("Index", new{office = office});
}

看法

<div class="row">
    <form method="POST" asp-action="Upload" asp-controller="FloorPlan" enctype="multipart/form-data">
        <input class="form-control" name="office" type="hidden" value="@ViewContext.RouteData.Values["office"]"  />
        <input class="form-control" name="path" type="file" />
        <input class="form-control" type="submit" value="Upload">
    </form>
</div>

标签: c#formsasp.net-core-mvc

解决方案


感谢君康利的评论,我现在有了这个工作控制器

[HttpPost]
public IActionResult Upload(string office, IFormFile file)
{
    return RedirectToAction("Index", new{office = office});
}

推荐阅读