50

I have this field that for some reason when I click on submit, gets a validation message that the field is required.

[DisplayName("Total Budget:")]
public double Budget { get; set; }

@Html.EditorFor(model => model.account.Budget)
@Html.ValidationMessageFor(model => model.account.Budget)

public class Account
{
    [DisplayName("Total Budget:")]
    public double Budget { get; set; } //dropdown
}
Rey
  • 3,663
  • 3
  • 32
  • 55
user1662812
  • 2,531
  • 4
  • 23
  • 27

8 Answers8

118

The built-in DefaultModelBinder in MVC will perform required and data type validation on value types like int, DateTime, decimal, etc. This will happen even if you don't explicitly specify validation using someting like [Required].

In order to make this optional, you will have to define it as nullable:

public double? Budget { get; set; }
Jerad Rose
  • 15,235
  • 18
  • 82
  • 153
10

You have to add the following line in the application_start (global.asax)

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

Source: Unrequired property keeps getting data-val-required attribute

Community
  • 1
  • 1
Nahuel García
  • 101
  • 1
  • 2
  • Welcome to stack overflow! Your answer provides an alternative solution and is perfectly valid. However it is generally advisable to give more detail about that in your answer. For example you should note that this configuration line would affect all entities in application. You're welcome to edit your answer to add more details. – infiniteRefactor Apr 21 '17 at 15:33
  • 1
    Please note: 1) This is global and will affect all value types (including dates, and enums) and 2) this was removed from Aspnet Core – Erik Funkenbusch May 03 '18 at 22:36
8

double is a value type. Value types always contain a value, even if you did not set one. That value is the default value for it's type (in this case 0.0). All value types are treated as required by the framework. The only way around this is to create a custom model binder, but that will not prevent the model from containing the default value (because there is no way to say that it wasn't entered).

So even if you create a custom binder, when you process your model, you won't be able to tell if someone entered 0 or whether that was just the default value.

Thus, the only real solution is to change your view model to use a nullable type, such as Nullable<double> (shorthand is double?).

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • 3
    Annoying when you have a property that you want to have the default if it wasn't specified. I want my property to be 0, not null, if it was left blank, but MVC is automatically failing validation saying it was required. – xr280xr Sep 04 '15 at 17:41
2

You probably change Budget from a double to double?

You probably can try adding this attribute to the controller

BindExclude([Bind(Exclude="Budget")]) as well

Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188
2

Use Nullable or ? after attribute name.

0

Use [NotMapped] annotation , this removes the required validation in the flow you also use own display attributes

Mahesan Rv
  • 11
  • 2
0

I have just encountered the problem with a project migrated to .Net 6.0, suddenly some fields started to be required even if there's no data annotation for it.

For example, in

public class LoginModel
{
    [BindProperty]
    public InputModel Input { get; set; }

    [TempData]
    public string ErrorMessage { get; set; }
    ...
}

I got the error : "The ErrorMessage field is required", which have of course no sense at all.

The project has <Nullable>enable</Nullable> feature enabled, it seem to cause this.

Simply rewrite property to

 public string? ErrorMessage { get; set; }

To fix this (note the question mark)

AFract
  • 8,868
  • 6
  • 48
  • 70
0

You may also use:

 services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);
Gauravsa
  • 6,330
  • 2
  • 21
  • 30