I have an MVC 5 application. I am trying to apply validation using my viewmodel attributes. E.g. my fields are required. But for some reason, errors on empty fields are displayed initially, before the user can enter anything. Could you please explain?
@Html.ValidationSummary(false, "Please fix the errors:", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FileToUpload, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBox("FileToUpload", "", new { @type = "file", @class = "input-xlarge form-control" })
@Html.ValidationMessageFor(model => model.FileToUpload, "*", new { @class = "text-danger" })
</div>
</div>
...
public ActionResult Index()
{
return View();
}
I use my viewmodel only for validation purposes, so it is not passed to the view.