Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp mvc file upload (with validation)

My model:

My controller:

Before submit (file is selected)

After submit (file is cleared)

As you can see my model (TestFileUploadModel) and my controller action (FileUpload)

-> 3th screen before i submit: file is selected -> 4th screen after i submit: name field is required, but file is cleared..

Anyone who was an idea how to solve this? (or a workaround?)

like image 351
Jordy Avatar asked Feb 14 '26 06:02

Jordy


1 Answers

As stated in the comment by user bzlm you can check with JQuery if the file has value and prevent the submit of the form.

You can attach a function at the submit of your form and do this check

if ($("#MyInputFileField").val()) { 
  //do your things
}
else
{
  //PreventDefault and show error message
}

EDIT: Look here for an answer that should be similar to your question

for an example using jquery validation plugin check this

like image 120
Iridio Avatar answered Feb 16 '26 22:02

Iridio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!