mvc upload file with model - second parameter posted file is null

后端 未结 4 1288
南旧
南旧 2020-12-05 13:35

I have a simple model with 1 string property which I render on a simple view.

the view looks like the following:

@using (Html.BeginForm(\"UploadFile\         


        
4条回答
  •  臣服心动
    2020-12-05 14:05

    Why not add the uploaded files to your model like this:

    public class UploadFileModel 
    {
        public UploadFileModel()
        {
            Files = new List();
        }
    
        public List Files { get; set; }
        public string FirstName { get; set; }
        // Rest of model details
    }
    

    Then change your view to this:

    @using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { encType="multipart/form-data" }))
    {
        @Html.TextBoxFor(m => m.FirstName)
        

    @Html.TextBoxFor(m => m.Files, new { type = "file", name = "Files" })

    }

    Then your files will be posted back as follows:

    public ActionResult UploadFile(UploadFileModel model)
    {
        var file = model.Files[0];
        return View(model);
    }
    

提交回复
热议问题