This is because in Input field of filetype, does post something, even if there is no file... the trick is to check ContentLength of the first element in the IEnumerable of HttpPostedFileBase, and only run your foreach loop is there is something there with a ContentLength...
Dont run code if no media in file picker
Hello, i am using the content and media service, to create content and upload media.
All works fine. But when i dont pick a media/file it still goes in my foreach var file in model.uploadFiles.
my form view.
My model:
My controller:
How can i avoid to run my foreach (var file in model.UploadFiles) if there is no files in my input file picker.
Hi Christian
This is because in Input field of filetype, does post something, even if there is no file... the trick is to check ContentLength of the first element in the IEnumerable of HttpPostedFileBase, and only run your foreach loop is there is something there with a ContentLength...
You could have an extension method like so:
and then that would enable you to write:
...
regards
Marc
Hey Marc,
Thanks! That worked great!
is working on a reply...