Model is Invalid always - 每次使用EF Core添加数据时都会出现错误。

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

我使用的是ASP.Net Corre 3.1。我的模型是这样的

public class Review: BaseEntity
{
    [Required]
    [Range(1,10)]
    public float Rating { get; set; }

    [Required(ErrorMessage = "Movie Watch date should be given")]
    [Display(Name = "Date of Watch", Prompt = "Please Give Date of Watch the Movie")]
    public DateTime WatchDate { get; set; }

    //public virtual string UserId { get; set; }     //Not Needed because they are automatically handled by Dot.Net Core
    [Required]
    [ScaffoldColumn(false)]
    public virtual User User { get; set; }

    //public virtual string MovieId { get; set; }    //Not Needed because they are automatically handled by Dot.Net Core
    [Required]
    public virtual Movie Movie { get; set; }
}

基础实体 是这样的

public class BaseEntity
{
    //[MaxLength(255)]
    [Key, Required, Column(TypeName = "VARCHAR(255)")]
    [ScaffoldColumn(false)]
    public Guid Id { get; set; }

    [DataType(DataType.DateTime)]
    [ScaffoldColumn(false)]
    public DateTime CreatedDate { get; set; }

    [DataType(DataType.DateTime)]
    [ScaffoldColumn(false)]
    //[DatabaseGenerated(DatabaseGeneratedOption.Computed)] //Not Needed
    public DateTime? UpdatedDate { get; set; }
}

我的 查看 是这样的

<form asp-action="Create">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <div class="form-group">
        <label asp-for="Rating" class="control-label"></label>
        <input asp-for="Rating" class="form-control" />
        <span asp-validation-for="Rating" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="WatchDate" class="control-label"></label>
        <input asp-for="WatchDate" readonly type="text" placeholder="Click to set start-date" class="DatePicker form-control" id="start_date">
        <span asp-validation-for="WatchDate" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="Movie" class="control-label"></label>
        <select asp-for="Movie" asp-items="ViewBag.Movies" class="form-control">
            <option disabled selected>-- Please Select a Movie to Review --</option>
        </select>
        <span asp-validation-for="Movie" class="text-danger"></span>
    </div>
    <div class="form-group">
        <input type="submit" value="Create" class="btn btn-primary" />
    </div>
</form>

而我的 控制器 是这样的

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Rating,WatchDate,Movie")] Review review)
{
    if (ModelState.IsValid)
    {
        review.Id = Guid.NewGuid();
        _context.Add(review);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }
    //ViewBag.Movies and ViewData["Movies"] => Both are same
    ViewData["Movies"] = new SelectList(await _context.Movies.ToListAsync(), "Id", "Name", review.Movie);
    return View(review);
}

我得到我的视图显示错误信息 - "模型无效"。

有谁能帮助我吗?

asp.net asp.net-mvc razor pagedlist dot.net
1个回答
0
投票

你可以通过获取以下属性来检查哪些属性没有通过验证。错误但我会说这是 User 属性,该属性被标记为 Required 在你 Review 阶层 IdBaseEntity 而你的表单和绑定只有Rating, WatchDate和Movie。我建议添加单独的类来模拟评论创建数据,并为它设置单独的验证。

© www.soinside.com 2019 - 2024. All rights reserved.