Good day/night, dear SOers,
I stuck with dynamically added file uploads inside the form in ASP.NET MVC. I create the form with dynamically added controls (insert rows in the table). One of them is file upload. The controls are added via this jQuery function:
$('#addRow').on('click', function () {
var counter = $('.itemRow').length;
$('#itemstable').append('<tr class="itemRow"> <td><select required name="ItemsList[' + counter + '].ProgramNameShort">@foreach (SelectListItem item in ViewBag.VBprograms) { <option value="@item.Value">@item.Text</option>} </select></td>' +
'<td><input type="text" name="ItemsList[' + counter + '].ItemName" /></td>' +
'<td><input type="text" name="ItemsList[' + counter + '].ItemProducer" /></td>' +
'<td><input type="text" name="ItemsList[' + counter + '].ItemQty" /></td>' +
'<td><input type="text" name="ItemsList[' + counter + '].Additional" /></td>' +
'<td><input type="text" name="ItemsList[' + counter + '].Comments" /></td>' +
'<td><input type="file" name="ItemsList[' + counter + '].Image" /></td>' +
'</tr>');
counter++;
});
My controller receives ViewModel:
[HttpPost]
public ActionResult Create(CallViewModel callViewModel)
ItemsList with Image as [] byte
property is the class inside CallViewModel class. Later the object is saved in the database (DB First approach). As far as I understand, images can be uploaded as HttpPostedFileBase object (or IEnumerable for multiple files uploaded). But how can I handle file uploads uploaded inside such a list?
I cannot show all the code as it is multi-step multi-tab form with the table being just a part inside one of tabs. If any parts of the code need to be demonstrated I can update current question.
Many thanks in advance.
UPD Here's the relevant part of CallViewModel class:
public class CallViewModel
{
... //properties used in other tabs
public List<ItemClass> ItemsList { get { return _items; } }
private List<ItemClass> _items = new List<ItemClass>();
public class ItemClass
{ //Item
public Guid ItemID { get; set; }
[Required, Display(Name = "Наименование")]
public string ItemName { get; set; }
[Required, Display(Name = "Производитель")]
public string ItemProducer { get; set; }
[Required, Display(Name = "Количество")]
public int ItemQty { get; set; }
[Display(Name = "Комментарии")]
public string Comments { get; set; }
[Required, Display(Name = "Изображение")]
public HttpPostedFileBase Image { get; set; }
[Display(Name = "Дополнительно предоставляемые объекты")]
public string Additional { get; set; }
//Program
//[Required, Display(Name = "Полное название")]
//public string ProgramNameFull { get; set; }
[Required, Display(Name = "Краткое название")]
public string ProgramNameShort { get; set; }
List<Program> Programs { get; set; }
}
}