3

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?)

bzlm
  • 9,626
  • 6
  • 65
  • 92
Jordy
  • 978
  • 2
  • 10
  • 20
  • 1
    Validate client-side. On a round-trip to the server, the file cannot be preserved. – bzlm Sep 11 '11 at 15:35

1 Answers1

1

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

Community
  • 1
  • 1
Iridio
  • 9,213
  • 4
  • 49
  • 71
  • 3
    This is built into ASP.NET MVC. No need to re-invent the wheel. :) – bzlm Sep 11 '11 at 15:48
  • AFAIK the MVC doesn't validate the input type upload with an Html helper. At least not without the jquery.validation plugin. – Iridio Sep 11 '11 at 16:34
  • this looks exactly what i need! But do you have some code example? because i can't figure out how it works.. – Jordy Sep 12 '11 at 15:53
  • In my update, the second link I posted, point to a demo with jquery validation. If you mean how to intercept the form post look here http://api.jquery.com/submit/ – Iridio Sep 12 '11 at 16:49