I've recently had some issues with Create
and Update
methods in my ASP.NET Core MVC App.
My program uses simple database with two tables with User.RoleId
being a foreign key referencing Role.Id
, so there are two model classes which I presume are coded properly.
I examined many lines of my code, tried to fix that in many different ways and I found out, that the problem lies in if(ModelState.isValid)
conditional. This conditional was generated by the scaffolding tool, but when I removed it from either Create
or Update
methods, everything worked as I wanted.
Here are some questions I'd like to ask:
- Is it necessary to use
ModelState
validation conditional and what are possible risks if I decide to remove it? - How can I make a valid
ModelState
? I need to apply some changes in what?User.cs
orRole.cs
?DbContext? Or maybe
Create/Update` methods in controllers?
I'd rather skip that problem, because I will be the only one using that program and my teacher will rather assess how the program works from single user's POV than check its code. But for future projects (or maybe this one, if ModelState
validation is rather necessary!) I want to know better what to do in such case.
Here is my DbContext
:
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
namespace PRP_CRM.Model;
public partial class PrpDbContext : DbContext
{
public PrpDbContext()
{
}
public PrpDbContext(DbContextOptions<PrpDbContext> options)
: base(options)
{
}
public virtual DbSet<Role> Roles { get; set; }
public virtual DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=PRP_DB;Integrated Security=True");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Role>(entity =>
{
entity.ToTable("Role");
});
modelBuilder.Entity<User>(entity =>
{
entity.ToTable("User");
entity.Property(e => e.EncryptedPassword).HasMaxLength(200);
entity.Property(e => e.Login).HasMaxLength(50);
entity.Property(e => e.Name).HasMaxLength(50);
entity.Property(e => e.Surname).HasMaxLength(50);
entity.HasOne(d => d.Role).WithMany(p => p.Users)
.HasForeignKey(d => d.RoleId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_user_role");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
Role.cs
:
public partial class Role
{
public int Id { get; set; }
public string? Name { get; set; }
public virtual ICollection<User> Users { get; } = new List<User>();
}
User.cs
:
public partial class User
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public string Surname { get; set; } = null!;
public DateTime DateOfBirth { get; set; }
public string Login { get; set; } = null!;
public string EncryptedPassword { get; set; } = null!; // (actually it's not encrypted yet)
public int RoleId { get; set; }
public bool IsDeleted { get; set; }
public virtual Role Role { get; set; } = null!;
}
If there are any other parts of the code that might be involved in ModelState.IsValid
returning false
, please tell me so I can show you.