asp.net-mvc – 如何将其他表单数据与MVC文件上传一起传递?

我正在尝试在MVC中实现文件上传.我有以下代码可行.

@using (Html.BeginForm("ActioName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))
{         
       <div>
            <input type="file" name="file" />
            <input type="submit" value="OK" class="button" />
        </div>       
}

      [HttpPost]
      public ActionResult UploadFile(HttpPostedFileBase file)
       {
        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0)
        {
        //do something here...
        }
      }

现在我想添加一个下拉框(选择文件类型)并将该值与文件一起发送到Controller.我该怎么做(将其他表单数据与文件一起发送)?

最佳答案
您应该能够将它们添加到视图中,将它们包含在POST中并让MVC处理模型绑定:

@using (Html.BeginForm("ActioName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))
{         
       <div>
            <input type="file" name="file" />
            <select name="fileType">
               <option value="JPG">Photo</option>
               <option value="DOC">Word</option>
            </select>
            <input type="submit" value="OK" class="button" />
        </div>       
}

      [HttpPost]
      public ActionResult UploadFile(HttpPostedFileBase file, string fileType)
      {
        //Validate the fileType

        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0)
        {
        //do something here...
        }
      }

转载注明原文:asp.net-mvc – 如何将其他表单数据与MVC文件上传一起传递? - 代码日志