Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.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# 文件输入不将文件上载到MVC操作_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 5 - Fatal编程技术网

C# 文件输入不将文件上载到MVC操作

C# 文件输入不将文件上载到MVC操作,c#,asp.net,asp.net-mvc,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,对于一个项目,我一直试图将一个文件从我的视图上传到我的控制器操作。 但是,由于某些原因,操作已到达,但对于该文件,我收到一个空值 这是我的密码: 行动: [HttpPost] [Route("UploadSickNote")] public ActionResult UploadSickNote(HttpPostedFileBase file) { // Do something with file return RedirectToAction("

对于一个项目,我一直试图将一个文件从我的视图上传到我的控制器操作。 但是,由于某些原因,操作已到达,但对于该文件,我收到一个空值

这是我的密码:

行动:

[HttpPost]
[Route("UploadSickNote")]
public ActionResult UploadSickNote(HttpPostedFileBase file)
{
    // Do something with file

    return RedirectToAction("SickNotes");
}
[HttpPost]
[Route("UploadSickNote")]
public ActionResult UploadSickNote(SickNotesViewModel vm)
{
    // Do something with file

    return RedirectToAction("SickNotes");
}
视图:

视图模型:

public class SickNotesViewModel
{
    public List<SickNoteElement> SickNoteElements { get; set; }
    public HttpPostedFileBase UploadedSickNote { get; set; }
}
公共类SickNotesViewModel
{
公共列表元素{get;set;}
公共HttpPostedFileBase UploadedSickNote{get;set;}
}
我使用asp.net mvc 5(不是asp.net core)和一些vue.js来实现查看功能。 非常感谢您的任何想法或提示。

编辑:

您必须确保输入名称与控制器匹配,以便

<input id="sicknote-upload" name="file" asp-for="UploadedSickNote" type="file" accept="image/*" capture>

如果这样做不起作用,您还可以尝试指定要发送的表单类型

<form Controller="Documents" Action="UploadSickNote" method="post" enctype="multipart/form-data">

编辑:

您必须确保输入名称与控制器匹配,以便

<input id="sicknote-upload" name="file" asp-for="UploadedSickNote" type="file" accept="image/*" capture>

如果这样做不起作用,您还可以尝试指定要发送的表单类型

<form Controller="Documents" Action="UploadSickNote" method="post" enctype="multipart/form-data">

在进一步搜索后找到了解决方案。 通过将视图更改为以下方式,我现在可以在我的viewmodel中接收文件:

   @using (Html.BeginForm("UploadSickNote", "Documents", FormMethod.Post, new { enctype = "multipart/form-data" }))
   {
            <div>
                <input id="sicknote-upload" name="UploadedSickNote" type="file" accept="image/*" capture>
                <input id="sicknote-button-submit" type="submit"/>
            </div>
   }
@使用(Html.BeginForm(“UploadSickNote”,“Documents”,FormMethod.Post,new{enctype=“multipart/form data”}))
{
}

在进一步搜索后找到了解决方案。 通过将视图更改为以下方式,我现在可以在我的viewmodel中接收文件:

   @using (Html.BeginForm("UploadSickNote", "Documents", FormMethod.Post, new { enctype = "multipart/form-data" }))
   {
            <div>
                <input id="sicknote-upload" name="UploadedSickNote" type="file" accept="image/*" capture>
                <input id="sicknote-button-submit" type="submit"/>
            </div>
   }
@使用(Html.BeginForm(“UploadSickNote”,“Documents”,FormMethod.Post,new{enctype=“multipart/form data”}))
{
}

Fromform用于asp.net核心。我现在不允许使用它。我的其他接收viewmodels的操作都可以在不使用fromform的情况下工作。我的坏消息我会相应地编辑,你必须在你的视图中更改一些内容,然后第二个代码片段也可以工作,我会接受它作为答案,因为它比我发现的更漂亮,感谢技术上说它们是一样的,@Html.BeginForm只是表单标记的包装,如果您检查代码,您将看到相同的结果,但我不喜欢代码中Html包装的外观,因此这只是个人偏好:)Fromform用于asp.net core。我现在不允许使用它。我的其他接收viewmodels的操作都可以在不使用fromform的情况下工作。我的坏消息我会相应地编辑,你必须在你的视图中更改一些内容,然后第二个代码片段也可以工作,我会接受它作为答案,因为它比我发现的更漂亮,感谢技术上说它们是一样的,@Html.BeginForm只是表单标签的包装,如果您检查代码,您将看到相同的结果,但我不喜欢代码中Html包装的外观,所以这只是个人偏好:)