如何将其他表单数据与MVC文件上传一起传递?

Gad*_*dam 4 asp.net-mvc file-upload form-data dropdownbox asp.net-mvc-controller

我正在尝试在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...
        }
      }
Run Code Online (Sandbox Code Playgroud)

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

And*_*NET 10

您应该能够将它们添加到视图中,将它们包含在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...
        }
      }
Run Code Online (Sandbox Code Playgroud)

  • 我尝试了上面的方法,文件总是变为空....任何想法 (3认同)