2

I have a Model Like this

public int Id {get;set;}

[Required]
public string FirstName{get; set}
[Required]
public string LastName{get; set}

The Id is auto generate in DB. when I want call Create action The ModelState says that "The Id field is required"!!!! I Found this for my problem but it not clean solution. Is there an other way to solve this?

Is there a way that I change Mvc ModelBinder behavior for value types?

Community
  • 1
  • 1
M.Azad
  • 3,673
  • 8
  • 47
  • 77
  • You sure you dont have forgotten to set auto increment on the column? – Lasse Edsvik Jun 17 '13 at 08:19
  • possible duplicate of [Removing REQUIRED Attribute from Class but MVC3 will NOT post the form without a value in the text box... Please HELP!](http://stackoverflow.com/questions/6814853/removing-required-attribute-from-class-but-mvc3-will-not-post-the-form-without-a) the question may seem unrelated but the accepted answer applies for your question as well. – nemesv Jun 17 '13 at 08:25
  • @LasseEdsvik Yes it generate in DB.If Id value is 0 the ModelState.Isvalid is false – M.Azad Jun 17 '13 at 08:25
  • Possible duplicate of [Unrequired property keeps getting data-val-required attribute](https://stackoverflow.com/questions/4700172/unrequired-property-keeps-getting-data-val-required-attribute) – KyleMit Dec 05 '17 at 18:22

3 Answers3

8

The best solution to this problem is to use a view model. A view model is a class that you specifically design to meet the requirements of your view. So your controller action will take this view model as parameter. Simply stop passing your domain models to your views. That's it.

And if you don't want to follow good practices and use view models you could disable this implicit validation by adding the following to your Application_Start:

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

another hack is to exclude this property from binding using the [Bind(Exclude = "Id")] attribute. Yeah, it's a hack but if you don't follow good practices you will have to live with hacks.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • @Darin Dimitrov I set add your code in Application_Start but ModelState.Isvalid is False;my Model like this : public class Person { public string FirstName { get; set; } public long Id { get; set; } } – M.Azad Jul 03 '13 at 15:41
  • What if the integer being binded determines your business logic? I.e if Id is set then update, if Id is not set then add. Would you go for nullable int? – marvc1 Oct 16 '13 at 10:11
1

Although @DarinDimitrov suggested a good option to use View Models, In addition to that answer. Also, consider this option only when you don't want to create the View-Model

How about ModelState["Id"].Errors.Clear(); in your Post Action Method ?

wuhcwdc
  • 286
  • 1
  • 10
  • 1
    That is worse than the `[Bind(Exclude="Id")]` hack, suggested by @Darin Dimitrov. The best practice is to *always* use a `ViewModel` for your views. That helps avoid this and similar issues. – Yakimych Jun 17 '13 at 13:56
0

You can also set the property type to Nullable<T>, and then just manually enforce any required validation you might need to do before working with the database.

public int? Id {get;set;}

Further Reading:

KyleMit
  • 30,350
  • 66
  • 462
  • 664