How to keep input type=file field value after failed validation in ASP.NET MVC?

前端 未结 7 1814
闹比i
闹比i 2020-11-27 04:39

I\'ve got a simple form in an MVC app I\'ve made. It contains a file field so users can upload an image. It all works great.

Problem is, if the form submit fails val

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 04:54

    You cannot set the value of a HTML file input box. As a workaround, replace the file upload box with a hidden input field when outputting the form after validation.

    On submission, you populate the hidden field with the value from the file input box (to be resubmitted later). Remember to have either the file upload or hidden field name present at any one time (not both):

    Note: The code below is for illustration/explanation purposes only. Replace it with the code appropriate to the language you use.

    
    
    
    
    
    

提交回复
热议问题