Viewmodel-related file upload

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;} //Other properties public List<FileUploadPacket> FileUploadPackets { 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;} //some more other properties public HttpPostedFileBase UpFile { 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...

+6
source share
2 answers

Fixed this change in the username and identifier of the download control.

 <%: Html.TextBoxFor(model => model.EmpName, new { maxLength = 50 })%> Upload your files here: <input type="file" id="FileUploadPackets[0].UpFile" name="FileUploadPackets[0].UpFile" value="ActionHandlerForForm" /> <%: Html.TextBoxFor(model => model.FileUploadPackets[0].UserEnteredDesc )%> <input type="file" id="FileUploadPackets[1].UpFile" name="FileUploadPackets[1].UpFile" value="ActionHandlerForForm" /> <%: Html.TextBoxFor(model => model.FileUploadPackets[1].UserEnteredDesc )%> 

It worked for me !! Hope this helps someone else.

+2
source

GetHtml helper is not part of the mvc structure, you should look for a third-party library containing this helper.

Downloading a file that is part of the ViewModel is simple. It basically looks like this

Define a presentation model

 public class MyViewModel { public HttpPostedFileBase MyFile { get; set; } } 

Inside Views/Shared/EditorTemplates create MyViewModel.cshtml

 <input type="file" id="MyFile" name="MyFile" /> 

And the view corresponding to the download action

 @model MyViewModel @using(Html.BeginForm("Upload", "MyController", FormMethod.Post, new { enctype="multipart/form-data"}) { @Html.EditorForModel() <input type="submit" value="Upload" /> } 

the required attribute is important for downloading files.

And so that he, after submitting the form, should see the downloaded file inside the action [HttpPost] , vm.MyFile .

+4
source

Source: https://habr.com/ru/post/916686/


All Articles