First create 2 ViewModels
public class NewViewModel
{
// list of files with additional data
public List<UploadItem> UploadItems { get; set; }
public string AnotherPropForView { get; set; }
}
public class UploadItem
{
// your additional data
public string CustomProp1 { get; set; }
public string CustomProp2 { get; set; }
// file
public HttpPostedFileBase UpFile { get; set; }
}
Create controller. One action for display empty form and second for process data from view.
public class SomeController : Controller
{
public ActionResult Create()
{
NewViewModel model = new NewViewModel
{
// inicialize list
UploadItems = new List<UploadItems>
{
// inicialize empty objects ( if you want to have 2 file fields with additional data)
// or inicialize only one object and another fields add by Javascript
new UploadItem {},
new UploadItem {},
}
}
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(NewViewModel model)
{
// if (ModelState.IsValid) etc...
foreach (var uploadItem in model.UploadItems)
{
// work with file and additional data
var file = uploadItem.UpFile;
var prop1 = uploadItem.CustomProp1;
// file.SaveAs("/some/where"); atc ...
}
// return some view...
}
}
And create view
@model Path.To.NewViewModel
@using (Html.BeginForm("Create", "Some", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@* Print all inicialized upload items *@
@for (int i = 0; i < Model.UploadItems.Count; i++)
{
@Html.TextBoxFor(model => model.UploadItems[@i].CustomProp1)
@Html.TextBoxFor(model => model.UploadItems[@i].CustomProp2)
<input type="file" id="UploadItems[@i].UpFile" name="UploadItems[@i].UpFile" />
}
<button name="Action" type="submit" value="Save" class="btn">Save</button>
}
You can use Javascript for dynamic adding Uploaditem to form - you have to generate right index to name and ID property in inputs