1

Previously I was uploading multiple files independantly of my model like this:

MODEL:

 public class PoliciesAddViewModel
  {
 public List<PackageTypeItemViewModel> PackageTypeItems { get; private set; }

    public PoliciesAddViewModel() 
    {
      PackageTypeItems = new List<PackageTypeItemViewModel>();
    } 
  }

  public class PackageTypeItemViewModel
  {
    public string Name { get; set; }
    public bool IsRequired { get; set; }
    public int DisplaySequence { get; set; }
  }

VIEW:

@{
        int i = 1;
       }
       @foreach (var pick in Model.PackageTypeItems)
       {
        <div class="editor-label">
          <label for="@pick.Name.Trim()">
            @pick.Name.Trim():
          </label>
        </div>
        <div class="editor-field">
          <input class="text-box single-line" type="file" name="files" id="@String.Format("file_{0}", i)" data-val="@pick.IsRequired.ToString().ToLower()" data-val-required="Please select a file" />
            @Html.ValidationMessage("files")
        </div>
         {
           i = i + 1;
         }

CONTROLLER:

 [HttpPost]
        public ActionResult Add(IEnumerable<HttpPostedFileBase> files, PoliciesAddViewModel model)
        {
         //blah
        }

This all works fine, but know I need to include other data with the files, most importantly an ID.

Ive tried updating my model like so:

  public class PackageTypeItemViewModel
  {
    public int PackageTypeItemId { get; set; }
    public HttpPostedFileBase File { get; set; }    
    public string Name { get; set; }
    public bool IsRequired { get; set; }
    public int DisplaySequence { get; set; }
  }

And my view:

 @for (int i = 0; i < Model.PackageTypeItems.Count; i++)
        {
         @Html.HiddenFor(m => m.PackageTypeItems[i].PackageTypeItemId)
        <div class="editor-label">
         @Html.LabelFor(m => m.PackageTypeItems[i].Name)
        </div>
        <div class="editor-field">
          <input class="text-box single-line" type="file" name="File" id="@String.Format("file_{0}", i)" data-val="@Model.PackageTypeItems[i].IsRequired.ToString().ToLower()" data-val-required="Please select a file" />
            @Html.ValidationMessage("files")
        </div>
        }

I have two isses, firstly and most importantly although the list is properly returned to my model, the file is null, and less importantly the name is being written simply as "name"

Any help would be great, if more info is required please post a comment

4

1 回答 1

3

您必须像这样更改视图中的文件输入,以启用模型绑定器将您的文件输入绑定到HttpPostedFileBase模型内的属性。

<input class="text-box single-line" type="file" name="@String.Format("PackageTypeItems_{0}__File", i)"  id="@String.Format("PackageTypeItems[{0}].File", i)" data-val="@Model.PackageTypeItems[i].IsRequired.ToString().ToLower()" data-val-required="Please select a file" />

编辑:

如果您使用的是 MVC 4,您可以使用@Html.IdFor@Html.NameFor喜欢这样

 <input class="text-box single-line" type="file" name="@Html.NameFor(m => m.PackageTypeItems[i].File)"  id="@Html.IdFor(m => m.PackageTypeItems[i].File)" data-val="@Model.PackageTypeItems[i].IsRequired.ToString().ToLower()" data-val-required="Please select a file" />
于 2013-05-05T13:53:58.190 回答