I have a form where I upload several files, and there are several text fields and some checkboxes associated with each downloaded file. I saw examples for downloading multiple files, where the actionresult signature looks something like this:
[HttpPost] public ActionResult Upload(IEnumerable<HttpPostedFileBase> fileUpload)
However, I cannot find any example where I can upload multiple files, where my actionresult signature looks something like this:
[HttpPost] public ActionResult Upload(MyViewModel vm)
The reason I want this viewmodel to be submitted is because I consider it cleaner than using the FormCollection variable, and because I want every file to be uploaded and the data to be added along with its associated text fields grouped together using a List<FileUploadPacket>
which will be part of the ViewModel
UPDATE
My view model below:
public class EmployeeVM { public int EmployeeID {get ;set;} public string EmpName {get ;set;}
The FileUploadPacket class, which has a property of type HttpPostedFileBase
public class FileUploadPacket { public int FileID {get ;set;} public string UserEnteredDesc {get ;set;}
Code snippet of my view.aspx as below
<%: Html.TextBoxFor(model => model.EmpName, new { maxLength = 50 })%> Upload your files here: <input type="file" id="UpFile" name="UpFile" value="ActionHandlerForForm" /> <%: Html.TextBoxFor(model => model.FileUploadPackets[0].UserEnteredDesc )%> <input type="file" id="UpFile" name="UpFile" value="ActionHandlerForForm" /> <%: Html.TextBoxFor(model => model.FileUploadPackets[1].UserEnteredDesc )%>
As you can see, I have all the other properties specific to this download file stored in its own class. So in my form, an employee can enter his name and upload his files and provide some description and other data for each file. If I move the public property HttpPostedFileBase UpFile { get; set; }
HttpPostedFileBase UpFile { get; set; }
HttpPostedFileBase UpFile { get; set; }
to the EmployeeVM
class, then I will have to collect all the files separately in the array and manually map the file to its description. Is there no way to do this by storing the UpFile
property in the FileUploadPacket
class FileUploadPacket
?
I am using aspx viewer.
Please, help. Thank you for your time...