Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Getting a 'property field is required' error despite having no [Required] attribute

I have this code:

@this.Html.DropDownListFor(vm => vm.FishId, 
    new SelectList(this.Model.Fishies, "FishId", "FishName", this.Model.FishId), "Please Select a Fish")

@this.Html.ValidationMessageFor(vm => vm.FishId)

The model for this is simply

public int FishId
{
    get;
    set;
}

So there is no validation here. When I press submit, I get a message saying The FishId Field is Required.. This is a client-side validation error. Any idea what's causing this?

If I change the validation message for, to this:

@this.Html.ValidationMessageFor(vm => vm.FishId, "Gotta select a fish, man")

Then the error message changes (which is what I want) to the right of the dropdown, however the validation summary still displays the original message.

All of my other stuff I put validation and messages either as RequiredField(...) attributes, or in a custom validation method.

What's going on, how can I change the validation summary message?

like image 716
NibblyPig Avatar asked Dec 03 '22 06:12

NibblyPig


2 Answers

Try put the int to nullable

public int? FishId { get; set; } 

It should pass validation because you allow null values

like image 152
Iridio Avatar answered Dec 27 '22 10:12

Iridio


Not sure if you want to disable the validation or change the error message. If the latter, then you should try adding the below to your model.

[Required(ErrorMessage="Gotta select a fish, man")]
public int FishId {get;set;}
like image 24
Paweł Staniec Avatar answered Dec 27 '22 10:12

Paweł Staniec