Question

I'm having file browse button like the following,most of the validation are done in the UI with the model annotation but some of them are done in the action (in the controller).

In this case when I go to the controller and do the validation for example and send the error to the page, In the UI you see message that no file chosen(I guess it because it calling to the following code for second time),How should I avoid that ?

Thanks,

<div class="form-group">
            @Html.LabelFor(model => model.Sat, new { @class = "control-label col-md-2" })
            <div class="col-md-10"> 
 <input type="file" name=@SConstants.SAFile />
            @Html.ValidationMessageFor(model => model.Sa)
            </div>
        </div>
Was it helpful?

Solution

When you do the round trip for the validation, the uploaded file gets lost. Even if you somehow preserve it, you cant set the value for file upload control for security reasons as mentioned in these answers: File upload answer 1 and File upload answer 2

The workaround for this is to use client side validation. If you are using jQuery, you can take a look uses of jQuery validations in these samples: Sample1 and Sample2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top