Asked by:
Dynamically change error message from controller

Question
-
User-29703693 posted
I'm trying to validate documents the using the following:
ViewModel:
[Remote("IsValidDocument", "Home", HttpMethod = "POST")] public IFormFile Doc1 { get; set; } [Remote("IsValidDocument", "Home", HttpMethod = "POST")] public IFormFile Doc2 { get; set; }
View:
<label asp-for="Doc1" class="control-label"></label> <input asp-for="Doc1" type="file" class="form-control" /> <span asp-validation-for="Doc1" class="text-danger"></span> <label asp-for="Doc2" class="control-label"></label> <input asp-for="Doc2" type="file" class="form-control" /> <span asp-validation-for="Doc2" class="text-danger"></span>
Home Controller:[HttpPost] public JsonResult IsValidDocument (object value) { int MaxContentLength = 1024 * 1024 * 10; //Max 10 MB file string[] AllowedFileExtensions = new string[] { ".doc", ".docx", ".odf", ".pdf", ".rtf", ".txt" }; var file = value as IFormFile; if (!AllowedFileExtensions.Contains((file != null) ? file.FileName.Substring(file.FileName.LastIndexOf('.')).ToLower() : string.Empty)) { ModelState.AddModelError(value.ToString(), "Please upload a document of type: " + string.Join(", ", AllowedFileExtensions)); return Json(false); } else if (file.Length > MaxContentLength) { ModelState.AddModelError(value.ToString(), "Your document is too large, maximum allowed size is : " + (MaxContentLength / 1024).ToString() + "MB";); return Json(false); } else return Json(true); }
However, ModelState.AddModelError is not outputting the error message. I'm pretty sure it's because the key parameter is off. What can I do to show the proper error messages?
Monday, October 14, 2019 5:49 PM
All replies
-
User2053451246 posted
Do you have @Html.ValidationSummary() on your page?
Monday, October 14, 2019 8:25 PM -
User1120430333 posted
I believe you would have to use the ValidationSummary in the view to show any error messages that were set by you manually setting error messages using ModelState.AddModelError() in the error collection. What are you doing if the ModelState is valid? Are you checking for the Modelstate being valid, becuase if so, it will remain invalid until you remove all messages you have placed into the error message collection used by ModelState logic?
@using (Html.BeginForm()) { @Html.ValidationSummary(false, "", new { @class = "text-danger" }) <fieldset> <legend>Create</legend>
Monday, October 14, 2019 8:32 PM -
User-29703693 posted
Thanks for the replies. I was having problems with the JsonResult script so I wound up creating a Validation Attribute instead. I haven't tested extensively, but this seems to be working for the most part and is easy to return different error messages.
using System; using System.Linq; using Microsoft.AspNetCore.Http; using System.ComponentModel.DataAnnotations; namespace Cwec.Attributes { public class ValidateDocumentAttribute : ValidationAttribute { public override bool IsValid(object value) { int maxContentLength = 1024 * 1024 * 10; //Max 10 MB file string[] allowedFileExtensions = new string[] { ".doc", ".docx", ".odf", ".pdf", ".rtf", ".txt" }; var file = value as IFormFile; if (file == null) return true; else if (!allowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.')).ToLower())) { ErrorMessage = "Please upload a document of type: " + string.Join(", ", allowedFileExtensions); return false; } else if (file.Length > maxContentLength) { ErrorMessage = "Your document is too large, maximum allowed size is : " + (maxContentLength / 1024).ToString() + "MB"; return false; } else return true; } } }
Tuesday, October 15, 2019 12:37 AM -
User-2054057000 posted
Dynamically change error message from controller - In order to do this I suggest you to use Custom Model Validation Attribute in your application.
Tuesday, October 15, 2019 5:49 AM