6

I have a model class "Country" with property "CultureId" that is NOT marked as required. Another class "CountryViewModel" holds the same property "CultureId".

When rendering the "Create" view, I noticed that validation data attributes were added to the "CultureId" textbox, although there were no data annotations added.

I am using

@Html.HiddenFor(mode => mode.CultureId)

What might be the cause for such a behavior?

Thanks

4 Answers 4

12

I'm guessing your CultureId is an int. MVC automatically adds required tags to non-nullable value types.

To turn this off add

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; 

to Application_Start or make your int nullable

Sign up to request clarification or add additional context in comments.

3 Comments

Already added that and still the same behavior. I will try to make the int nullable. Thanks
Good answer. It works to get rid of the data-required validation, but then since the Id is an int it adds the data-val-number which still prevents form submission when trying to create. I could work around it by setting default values like 0, but I shouldn't have to.
My bad - it works as the validation seems to skip data-val-number for hidden inputs
9

There are couple of ways to handle this -
a). Make the property as Nullable like

public int? Age  { get; set; }

b). Use the below in controller class -

ModelState["Age"].Errors.Clear();

c). Add to the startup- DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

1 Comment

Does option (b) solve client-side validation behaviour? No right, since you're clearing the errors on the postback?
0

if you can use data annotations, you should check out this http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx

namespace MvcApplication1.Models
{
[MetadataType(typeof(MovieMetaData))]
public partial class Movie
{
}


public class MovieMetaData
{
    [Required]
    public object Title { get; set; }

    [Required]
    [StringLength(5)]
    public object Director { get; set; }


    [DisplayName("Date Released")]
    [Required]
    public object DateReleased { get; set; }
}

}

it helps you set validations without models on database side.

2 Comments

His problem is not about how to set required on a property, but how to avoid the undesired html validation he is running into. Moreover your way of managing attribute is typical of dynamic data, not the way MVC usually works
i thought it will be override validation rules, if metadata class will be empty
0

try this:

ModelState["CultureId"].Errors.Clear();

if (ModelState.IsValid)     
    {
         .....
    }

If CultureId is int then it will also give you the desired result...

1 Comment

This is a bit of a kludge that avoids the real issue involved here. See the answer from dskh for more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.