[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?
Do you have @Html.ValidationSummary() on your page?
Mark all posts that give the desired result the answer. If you only mark the last that gave you clarification because you misread an earlier post others will be confused. Some of us are here to help others and our point to post ratio matters.
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?
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;
}
}
}
Dynamically change error message from controller - In order to do this I suggest you to use
Custom Model Validation Attribute in your application.
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Member
116 Points
350 Posts
Dynamically change error message from controller
Oct 14, 2019 05:49 PM|bank5|LINK
I'm trying to validate documents the using the following:
ViewModel:
Home Controller:
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?
Contributor
7048 Points
2188 Posts
Re: Dynamically change error message from controller
Oct 14, 2019 08:25 PM|ryanbesko|LINK
Do you have @Html.ValidationSummary() on your page?
Contributor
4923 Points
4198 Posts
Re: Dynamically change error message from controller
Oct 14, 2019 08:32 PM|DA924|LINK
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?
Member
116 Points
350 Posts
Re: Dynamically change error message from controller
Oct 15, 2019 12:37 AM|bank5|LINK
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.
Participant
1253 Points
935 Posts
Re: Dynamically change error message from controller
Oct 15, 2019 05:49 AM|yogyogi|LINK
Dynamically change error message from controller - In order to do this I suggest you to use Custom Model Validation Attribute in your application.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠