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