Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC: DataAnnotations - Show an error message indicating that a field must be numeric
    primarykey
    data
    text
    <p>There appears to be something of a hole in the way DataAnnotations works in that a user entering in some text into a field that will go into an int will never reach the DataAnnotations code. It kicks off a model binding error and displays the error to the user "The value 'a' is not valid for the XXXX field."</p> <p>Anyway, it's all very nice that it automatically handles this situation, but I actually want to display an error message indicating the problem eg. "The value 'a' is not numeric. Please enter in a numeric value for the XXXX field".</p> <p>I have tried the solutions set out <a href="https://stackoverflow.com/questions/1538873/how-to-replace-the-default-modelstate-error-message-in-asp-net-mvc-2">How to replace the default ModelState error message in Asp.net MVC 2?</a> and <a href="https://stackoverflow.com/questions/646270/asp-net-mvc-custom-validation-message-for-value-types/1374653#1374653">ASP.NET MVC - Custom validation message for value types</a>, but I can't get them to work.</p> <p>It appears that my resource file is not being read at all, since here (<a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultmodelbinder.resourceclasskey.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultmodelbinder.resourceclasskey.aspx</a>) it states "If the property is set to an invalid class key (such as a resource file that does not exist), MVC throws an exception." and even if I change the line to DefaultModelBinder.ResourceClassKey = "asdfasdhfk" there is no exception.</p> <p>Anyone have any ideas?</p> <p>EDIT: Here is some code. All of it is working minus my Messages.resx file's messages are not being used. The code for Messages.resx is auto generated so I won't include it.</p> <p>So entering "a" into ProcessOrder results in a generic message rather than what I have entered into Messages.resx for PropertyValueInvalid (and InvalidPropertyValue for good measure).</p> <p>Application_Start method</p> <pre><code>protected void Application_Start() { RegisterRoutes(RouteTable.Routes); ModelBinders.Binders.DefaultBinder = new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder(); //set dataanooations to be used DefaultModelBinder.ResourceClassKey = "Messages"; //set data annotations to look in messages.resx for the default messages ValidationExtensions.ResourceClassKey = "Messages"; } </code></pre> <p>Entity Class</p> <pre><code>[MetadataType(typeof(GLMetaData))] public partial class GL { } public class GLMetaData { public int TransRefId { get; set; } [DisplayName("Process Order")] public int? ProcessOrder { get; set; } [DisplayName("Trans Type")] [StringLength(50)] public string TransType { get; set; } [StringLength(100)] public string Description { get; set; } [DisplayName("GL Code")] [StringLength(20)] public string GLCode { get; set; } [DisplayName("Agents Credit No")] [StringLength(50)] public string AgentsCreditNo { get; set; } [Required] public bool Active { get; set; } } </code></pre> <p>Controller Action:</p> <pre><code> [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(GL glToBeUpdated) { try { if (!ModelState.IsValid) return View(glToBeUpdated); //set auto properties glToBeUpdated.UpdateDate = DateTime.Now; glToBeUpdated.UpdateUser = this.CurrentUser; glDataLayer.update(glToBeUpdated); glDataLayer.submitChanges(); return RedirectToAction("Index"); } catch { glDataLayer.abortChanges(); throw; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload