ModelState.IsValid == false 因为两个表之间的关系无效的字段

问题描述 投票:0回答:1

我有很多桌子。电影,演员,电影演员,...。演员表和电影表之间的关系是多对多的。 我使用 asp.net core 6 Code First 。 这是演员表的代码:

public class Actor
    {
        [Key]
        public int ActorId { get; set; }
        [DisplayName("Image")]
        public string? ProfilePictureURL { get; set; }
        [NotMapped]
        public IFormFile? PicUpload {  get; set; }
        [Required(ErrorMessage =ErrorMsg.RequiredMsg)]
        [DisplayName("FullName")]
        //[StringLength(50,MinimumLength = 6,ErrorMessage = ErrorMessage.StringLength)]

        public string FullName { get; set; }
        [Required(ErrorMessage = ErrorMsg.RequiredMsg)]
        [DisplayName("Biography")]
        public string Bio { get; set; }
        public List<MovieActor> MovieActors { get; set; }
    }

和电影

public class Movie
    {
        public int MovieId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
     
        public MovieCategory MovieCategory { get; set; }
        public string? ImageURL { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public decimal Price { get; set; }

        public ICollection<MovieActor> MovieActors { get; set; }
        public ICollection<MovieCinema> MovieCinemas { get; set; }

        public int MovieDetailId { get; set; }
        [ForeignKey("MovieDetailId")]
        public MovieDetail MovieDetail { get; set; }
        
    }

和 MovieActor 表

public class MovieActor
    {
        public int MovieId { get; set; }
        public int ActorId { get; set; }
        public Movie Movie { get; set; }
        public Actor Actor { get; set; }
    }```
The problem is when I want to create an actor ModelState is not valid because MovieActor validation is invalid on  Actor table

The Create.cshtml
<div class="row">
            <div class="col-md-8 offset-2">
                <form asp-action="Create" enctype="multipart/form-data" >
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                    <div class="form-group text-center">
                        <img id="ProfilePicturePreview" />
                    </div>
                    <div class="form-group">
                        <label asp-for="FullName" class="control-label"></label>
                        <input asp-for="FullName" class="form-control" />
                        <span asp-validation-for="FullName" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label asp-for="Bio" class="control-label"></label>
                        <input asp-for="Bio" class="form-control"/>
                        <span asp-validation-for="Bio" class="text-danger"></span>
                    </div>
                    <div class="form-group">                      
                        <input asp-for="PicUpload"  />
                    </div>
                    <div class="form-group">
                        <input type="submit" value="Create" class="btn btn-outline-success float-right " />
                        <a class="btn btn-outline-secondary" asp-action="Index" >Show All</a>
                    </div>
                </form>
            </div>
        </div>

这里在 ActorsController 处创建

public async  Task<IActionResult> Create(){return View();}
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("FullName,Bio")]Actor actor)
    {
        //Upload Picture
        if (actor.PicUpload?.Length > 0)
        {
            string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images", "actors",
                Guid.NewGuid().ToString() + "_" + actor.PicUpload.FileName + Path.GetExtension(actor.PicUpload.FileName));
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                actor.PicUpload.CopyTo(stream);
            }
            actor.ProfilePictureURL = filePath;
        }

        if (!ModelState.IsValid) 
        {
            return View(actor);
        }
        await _service.Add(actor);
        return RedirectToAction("Index");
    }

我想添加一个 Actor,但模型状态无效。请帮我找到并解决问题。

asp.net-core many-to-many crud relation modelstate
1个回答
0
投票

从 .NET 6 开始,不可为 null 的属性必须是必需的,否则 ModelState 将无效。

第一种方法是您可以从项目文件中删除

<Nullable>enable</Nullable>
(双击项目名称或右键单击项目选择“编辑项目文件”)。

第二种方式是在属性中添加

?

public class Actor
{
    [Key]
    public int ActorId { get; set; }
    [DisplayName("Image")]
    public string? ProfilePictureURL { get; set; }
    [NotMapped]
    public IFormFile? PicUpload {  get; set; }
    [Required(ErrorMessage =ErrorMsg.RequiredMsg)]
    [DisplayName("FullName")]
    //[StringLength(50,MinimumLength = 6,ErrorMessage = ErrorMessage.StringLength)]

    public string FullName { get; set; }
    [Required(ErrorMessage = ErrorMsg.RequiredMsg)]
    [DisplayName("Biography")]
    public string Bio { get; set; }
    public List<MovieActor>? MovieActors { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.