Where does this error message "A value is required." come from?

Topics: Validation Application Block
Apr 24, 2009 at 12:56 PM
Hi everybody,

I use VAB with an asp.net MVC application. I want to validate a form of a strongly typed view before inserting in database. This is a code to illustrate :

namespace NS.Entities
  public class Stuff
    [RangeValidator(1, RangeBoundaryType.Inclusive, 9999, RangeBoundaryType.Inclusive, MessageTemplate="Invalid size.")]
    public int Size { get; set; }

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NS.Entities.Stuff>" %>
<%@ Import Namespace="NS.Common.Enums" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<form id="create" method="post" action="Create">
<p><label for="size">size</label>
  <input id="size" type="text" name="size" />
<button id="submit" type="submit" value="save" name="submit">save</button>
So, I decorate the property with a [RangeValidator] attribute. The problem is, if the form field is empty I get two error messages :
  • A value is required.
  • Invalid size.
I don't understand why I get "A value is required". Does somebody can help me?

Thanks by advance.
Apr 27, 2009 at 8:54 AM

I tried doing this scenario on a regular asp.net but was not able to reproduce the problem. Instead of getting the 2 error messages, I got an exception. "Index was outside the bounds of the array.". But when I input something larger or less than the range, it validates just fine. I'm not sure why does it happen or where the other message came from, so, I take a look at the source code of the asp.net mvc, and I think it has something to do with the DefaultModelBinder which uses that message. Also I'm not familiar with asp.net mvc. Would you mind sending us your sample solution so we can take a closer look.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
Apr 27, 2009 at 10:47 AM

Thanks for your reply.

How can I send you the solution? Can I send it to your email address with the title of the discussion?
Apr 27, 2009 at 11:01 AM
entlib.support@avanade.com, Yes. Thanks.
Apr 30, 2009 at 7:14 AM
After debugging the solution you send, found out that that error message is not something generated from VAB.  If you breakpoint in the Create method of the StuffController at this line:

ValidationState<Stuff> results = new ValidationState<Stuff>(ModelState);

Inspect the ModelState.Values collection property, you'll see that it already contains that error message.  So even before the call to validate, you already have that error message. I'm no expert in MVC so I suggest you redirect your inquiry to other forums.  

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.


Apr 30, 2009 at 8:52 AM
Edited Apr 30, 2009 at 12:00 PM
Thanks a lot for that, it's a big step for me.