MVC4 Entity Framework 6 Model State Validation Issues

mituw16

I'm working on an MVC4 with EF6 project, and have run into a slightly small but frustrating problem. I have a situation where I have the [Required] attribute set in my dbcontext, however, I do want to allow said property to be okay with empty strings.

I have tried what was suggested in this article, http://www.dzone.com/articles/ef-code-firstmvc, as well as putting [DisplayFormat(ConvertEmptyStringToNull = false)] on my context properties.

When I POST from my login page, the First Name, Last Name, Email, Phone properties are null, which is throwing the ModelState out of whack, even though I've set it to allow those properties to be empty strings. Am I missing something?

Model / DBContext

public class User : Entity
{
    [StringLength(200)]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    [Required]
    public String UserName { get; set; }

    [StringLength(250)]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    [Required]
    public String Password { get; set; }

    [StringLength(200)]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    [Required]
    public String FirstName { get; set; }

    [StringLength(200)]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    [Required]
    public String LastName { get; set; }

    [StringLength(200)]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    [Required]
    public String Email { get; set; }

    [StringLength(200)]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    [Required]
    public String Phone { get; set; }
}

Controller

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(UserViewModel Model)
{
    var _UM = Model.User;
    var User = _repo.GetSingle<User>(x => x.UserName == _UM.UserName);

    if(User != null)
    {
        if (Hash.ValidatePassword(_UM.Password, User.Password))
        {
            return RedirectToAction("Dashboard");
        }
        else
        {
            ModelState.AddModelError("InvalidPass", "Invalid Credentials");
         }
    }
    else
    {
        ModelState.AddModelError("NoUser", "Invalid Credentials");
    }

    return View(Model);
}

If invalid credentials are set, I would expect the ModelState keys to only have one of the items that I am explicity setting. However, it has 6 keys (First Name, Last Name, etc are required).

mituw16

I ended up solving this by moving my EF context configurations to use the Fluent API instead of Data Annotations.

Model / DBContext

public class User : Entity
{
    public String UserName { get; set; }

    public String Password { get; set; }

    public String FirstName { get; set; }

    public String LastName { get; set; }

    public String Email { get; set; }

    public String Phone { get; set; }
}

DBContext File

public class DBContext : DbContext
{

    public DBContext()
        : base("ConString")
    {

    }

    public DbSet<User> Users { get; set; }

    public DbSet<UserRole> UserRoles { get; set; }

    public DbSet<Region> Regions { get; set; }

    public DbSet<InboundFile> InboundFiles { get; set; }

    public DbSet<FileType> FileTypes { get; set; }


    //configure objects 
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<User>().Property(x => x.FirstName).IsRequired().HasMaxLength(200);
        modelBuilder.Entity<User>().Property(x => x.LastName).IsRequired().HasMaxLength(200);
        modelBuilder.Entity<User>().Property(x => x.Phone).IsRequired().HasMaxLength(200);
        modelBuilder.Entity<User>().Property(x => x.Email).IsRequired().HasMaxLength(200);
        modelBuilder.Entity<User>().Property(x => x.UserName).IsRequired().HasMaxLength(200);
        modelBuilder.Entity<User>().Property(x => x.Password).IsRequired().HasMaxLength(200);


    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MVC 4 Conditional Model Validation with Entity Framework

From Dev

Umbraco SurfaceController model state / model validation issues

From Dev

MVC3 Conditional Validation with Entity Framework Model First

From Dev

Entity Framework profiler - ASP.NET MVC4 with EF 6 -Unable to determine the provider name

From Dev

Single or multiple DbContext File in mvc4 using Entity framework 6

From Dev

Entity Framework profiler - ASP.NET MVC4 with EF 6 -Unable to determine the provider name

From Dev

Entity Framework 6 with MySQL - UniqueIdentifier issues

From Dev

PieChart in mvc4 from Entity Framework

From Dev

Dropdownlist and MVC4 Entity Framework

From Dev

How can i create a Partial View for MVC4 or MVC 5 using Entity Framework (.edmx Model) with Razor Views?

From Dev

MVC 4 Entity Framework Model is null

From Dev

How to extend an Entity Framework 6 model

From Dev

Entity Framework 6 Model First Migration

From Dev

Log4net with Entity Framework 5 and MVC4

From Dev

Entity Framework 6 Could not find the CLR type for <model>.<entity>

From Dev

Entity Framework 6 Code First Relationship/table creation issues

From Dev

Custom Server Side Validation MVC Entity Framework

From Dev

How to mock method with ViewModel in MVC4 with Entity Framework 4.0

From Dev

Will Orchard CMS support MVC4 with Entity Framework

From Dev

MVC4 Entity Framework Controller Database Issue

From Dev

MVC4 app - how to remove Entity Framework and implement SimpleMembershipProvider?

From Dev

How to mock method with ViewModel in MVC4 with Entity Framework 4.0

From Dev

Entity framework seed issues

From Dev

Entity Framework Deployment Issues

From Dev

Entity Framework 6 update with required complex property validation error

From Dev

Entity Framework 6 - GetObjectStateEntries expected modified entities have state "Unchanged"

From Dev

Child Model Validation using Parent Model Values. Fluent Validation. MVC4

From Dev

Child Model Validation using Parent Model Values. Fluent Validation. MVC4

From Dev

Dal (with Entity Framework) and Model layers into MVC

Related Related

  1. 1

    MVC 4 Conditional Model Validation with Entity Framework

  2. 2

    Umbraco SurfaceController model state / model validation issues

  3. 3

    MVC3 Conditional Validation with Entity Framework Model First

  4. 4

    Entity Framework profiler - ASP.NET MVC4 with EF 6 -Unable to determine the provider name

  5. 5

    Single or multiple DbContext File in mvc4 using Entity framework 6

  6. 6

    Entity Framework profiler - ASP.NET MVC4 with EF 6 -Unable to determine the provider name

  7. 7

    Entity Framework 6 with MySQL - UniqueIdentifier issues

  8. 8

    PieChart in mvc4 from Entity Framework

  9. 9

    Dropdownlist and MVC4 Entity Framework

  10. 10

    How can i create a Partial View for MVC4 or MVC 5 using Entity Framework (.edmx Model) with Razor Views?

  11. 11

    MVC 4 Entity Framework Model is null

  12. 12

    How to extend an Entity Framework 6 model

  13. 13

    Entity Framework 6 Model First Migration

  14. 14

    Log4net with Entity Framework 5 and MVC4

  15. 15

    Entity Framework 6 Could not find the CLR type for <model>.<entity>

  16. 16

    Entity Framework 6 Code First Relationship/table creation issues

  17. 17

    Custom Server Side Validation MVC Entity Framework

  18. 18

    How to mock method with ViewModel in MVC4 with Entity Framework 4.0

  19. 19

    Will Orchard CMS support MVC4 with Entity Framework

  20. 20

    MVC4 Entity Framework Controller Database Issue

  21. 21

    MVC4 app - how to remove Entity Framework and implement SimpleMembershipProvider?

  22. 22

    How to mock method with ViewModel in MVC4 with Entity Framework 4.0

  23. 23

    Entity framework seed issues

  24. 24

    Entity Framework Deployment Issues

  25. 25

    Entity Framework 6 update with required complex property validation error

  26. 26

    Entity Framework 6 - GetObjectStateEntries expected modified entities have state "Unchanged"

  27. 27

    Child Model Validation using Parent Model Values. Fluent Validation. MVC4

  28. 28

    Child Model Validation using Parent Model Values. Fluent Validation. MVC4

  29. 29

    Dal (with Entity Framework) and Model layers into MVC

HotTag

Archive