I have a view that has the following file input:
<input type="file" name="SchemaFiles" id="SchemaFiles" class="form-control" multiple/>
My model has this attribute:
public List<HttpPostedFileBase> SchemaFiles { get; set; }
When I select one or two files for this input, everything works fine. However, if I don’t select any files and submit the form, then SchemaFiles.Count is 1, and SchemaFiles[0] is null (but does exist). ModelState.IsValid is true.
Is there a way to avoid this?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This is by-design and looks like the root cause is the browser.
See these links for reference:
- ASP.NET work item: http://aspnetwebstack.codeplex.com/workitem/188
- Stackoverflow article: Why IEnumerable<HttpPostedFileBase> count is 1 when I upload 0 files? and
- Blog article: http://michaelsync.net/2014/04/29/asp-net-mvc-multiple-files-upload-bug-or-by-design-issue
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0