Reputation: 3361
I have an MVC 5 project with a form where the user can select multiple files to upload. However each file needs to have an additional integer value indicating its type. How do I implement this so the post method receives the collection of HttpPostedFileBase objects, along with each having the type value?
Upvotes: 1
Views: 451
Reputation: 980
It will "just work", as long as your POSTed data is in the right format. The MVC model binder should just hook things up. If you've already gotten your controller action to receive an array of HttpPostedFileBase
objects, you should just be able to have another argument to your action that is the array of integers `int[] fileTypes'. In your form, you will need input elements that contain those integers, with NAME attributes that allow the MVC model binder to bind the collection properly, which usually means you'd have input elements like this on your page:
<input type="hidden" value="3" name="fileTypes[0]" />
<input type="hidden" value="5" name="fileTypes[1]" />
With something like that you'd have a controller action that looked like
public ActionResult UploadMultipleFiles(HttpPostedFileBase[] uploadedFiles, int[] fileTypes)
The model binder will actually take care of hooking the input arguments up for you, as long as they were named properly on the HTML form.
If you are doing your upload via AJAX POST, the same still holds, you just have to make sure that you put the fileTypes values in as form variables with the right key names for collection binding.
Upvotes: 1