【问题标题】:How to pass other form data along with MVC File Upload?如何将其他表单数据与 MVC 文件上传一起传递?
【发布时间】:2013-12-03 09:10:15
【问题描述】:

我正在尝试在 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...
        }
      }

现在我想添加一个下拉框(以选择文件类型)并将该值与文件一起发送到我的控制器。我该怎么做(连同文件一起发送其他表单数据)?

【问题讨论】:

    标签: asp.net-mvc file-upload dropdownbox form-data asp.net-mvc-controller


    【解决方案1】:

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

    【讨论】:

    • 感谢奎蒂的回答。我赞成它,因为我相信它会对某人有所帮助。我只是继续并采取了使用模型的方法,以使其更整洁。
    • input 中的名称是否与操作方法中的文件参数名称匹配?在上面的示例中,两者都是名称“文件”。
    【解决方案2】:

    我最终按照以下方式进行操作。对我有用:

    创建了一个模型:

    public class FeeUpload
    {
        [Required (ErrorMessage="File Type required")]       
        public string fileType { get; set; }
    
        [Required (ErrorMessage="file required")]
        public HttpPostedFileBase File { get; set; }
    
    }
    

    查看:

    @using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))
    { 
        @Html.ValidationSummary(false, "Please fix the following:")
        <div>
           <div>
                @Html.DropDownListFor(model => model.fileType,
                                       new List<SelectListItem>
                                             {
                                                new SelectListItem{ Text="xxx", Value = "yyy" }, 
                                                new SelectListItem{ Text="xxx", Value = "yyy" },
                                                new SelectListItem{ Text="xxx", Value = "yyy" }
                                             }, "Select")
                @*@Html.ValidationMessageFor(model => model.fileType)*@
            </div>
    
            <div>
                @Html.TextBoxFor(model => model.File, new { type = "file" })
                @*@Html.ValidationMessageFor(model => model.File)*@  
                <input type="submit" value="OK" class="button" id="btnsubmit" />
            </div>
        </div>
    }
    

    控制器:

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult FeesAndCostsUpload(FeeUpload feeUploadFile)          
        {
            if (ModelState.IsValid)
            {
               //do something with feeUploadFile.File and feeUploadFile.fileType          
            }
            return View();
        }
    

    【讨论】:

      【解决方案3】:

      尽量不要在表单中使用 Razor

      <form method="POST" data-url="@Url.Action("Action", "Controller")" enctype="multipart/form-data">
              @Html.ValidationSummary(true)
      
              <span class="btn btn-success fileinput-button">
                  <i class="fa fa-plus"></i>
                  <span>Add a file...</span>
                  @Html.TextBoxFor(model => model.Fichier, new { type = "file" })
              </span>
      
              <div class="form-group form-actions">
                  <div class="col-sm-offset-3 col-sm-9">
                      <input id="submit" type="submit" class="btn btn-primary" value='Value' />
                  </div>
              </div>
          </form>
      

      为我工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-14
        • 2021-06-10
        • 2018-01-23
        • 2023-03-30
        • 2014-01-22
        • 1970-01-01
        • 2015-12-28
        相关资源
        最近更新 更多