Im trying to validate a dropdown list where you can select multiple entries. If you don't select any element it should show me an error message, when i try to submit the form. Why doesn't it work?
View
@Html.DropDownListFor(model =>
model.ChosenEmployeesIds,
Model.Employees,
new Dictionary<string, object>
{
{"class", string.Format("form-control chosen-select {0}", Model.IsUserAlleMA ? "defaultSelect": "")},
{"id", "EmployeesList"},
{"multiple", ""}
})
@Html.ValidationMessageFor(model => model.ChosenEmployeesIds, "", new { @class = "text-danger" })
Viewmodel
[Required, MinLength(1, ErrorMessage = "Bitte wählen Sie mindestens einen Mitarbeiter aus.")]
public IEnumerable<int> ChosenEmployeesIds { get; set; }
Controller
[HttpPost]
[ValidateAntiForgeryToken]
public FileResult DownloadByEmployee(DownloadsViewModel viewModel)
{
// Prepare zip name
var currentDate = DateTime.Now.ToString("yyyyMMdd");
var zipName = $"name{currentDate}.zip";
// Zip archive can write into a stream, memory stream is used to keep
// data only on the short-term memory (RAM)
using (var zipMemoryStream = new MemoryStream())
{
using (var zipArchive = new ZipArchive(zipMemoryStream, ZipArchiveMode.Create, false))
{
foreach (var employeeId in viewModel.ChosenEmployeesIds)
{
var userCertificates = _userCertificateRepository
.Get(uc => uc.EmployeeId == employeeId)
.ToList();
WriteUserCertificateFilesIntoZipArchive(zipArchive, userCertificates, true);
}
}
return File(zipMemoryStream.ToArray(), "application/zip", zipName);
}
}
CodePudding user response:
You need to check that the model state is valid in the controller action as well. If it isn't, return the view which should now show your validation message.
[HttpPost]
[ValidateAntiForgeryToken]
public FileResult DownloadByEmployee(DownloadsViewModel viewModel)
{
if(!ModelState.IsValid)
{
//return View(viewModel);
return View("Index", viewModel);
}
// Prepare zip name
var currentDate = DateTime.Now.ToString("yyyyMMdd");
var zipName = $"name{currentDate}.zip";
// Zip archive can write into a stream, memory stream is used to keep
// data only on the short-term memory (RAM)
using (var zipMemoryStream = new MemoryStream())
{
using (var zipArchive = new ZipArchive(zipMemoryStream, ZipArchiveMode.Create, false))
{
foreach (var employeeId in viewModel.ChosenEmployeesIds)
{
var userCertificates = _userCertificateRepository
.Get(uc => uc.EmployeeId == employeeId)
.ToList();
WriteUserCertificateFilesIntoZipArchive(zipArchive, userCertificates, true);
}
}
return File(zipMemoryStream.ToArray(), "application/zip", zipName);
}
}
CodePudding user response:
For client validation,you need to check if you have refered
<script src="~/lib/jquery/dist/jquery.min.js" ></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js" ></script>
notice not reserve the order
and set a value to errormessage in @Html.ValidationMessageFor()
@Html.ValidationMessageFor(model => model.ChosenEmployeesIds, "errormessage", new { @class = "text-danger" })
And it may help if you could share more details of your model and you could press F12 to observe the codes which has been complied to html and share it with us