ModelState.IsValid 返回 false 时出现问题

Trouble with the ModelState.IsValid returning false

ASP.NET中使用Entity Framework 我正在尝试向名为 Provider 的数据库添加一个新条目。视图 returns 一个默认值 UserID 和一个类别作为来自另一个 table 的外键。当我调试控制器时,Post 方法有一个 if (ModelState.IsValid) 不断返回 false。不明白为什么我可能不完全理解 ModelState 及其工作原理。

当我将 if(ModelState.IsValid) 重写为 if(true) 时,它成功地将新条目添加到数据库并正确设置了所有字段。

当我验证本地人时,它显示以下内容: (我选择将图片作为 link 放置,因为没有足够的声誉来使用图像格式) https://i.ibb.co/2Yrz4d8/debugging-ASP.png

ModelState.IsValid = false

providers             | {MAR.Models.Providers}
providers.ID          | 0
Providers.UserID      | "003f528f-f8..."
providers.VisitAvail  | null
providers.VisitType   | null
providers.VisitTypeID | 1

型号:

public class Providers
    {
        public int ID { get; set; }
        public string UserID { get; set; }
        public int? VisitTypeID { get; set; }
        public virtual VisitTypes VisitType { get; set; }
        public virtual ICollection<VisitAvailabilitys> VisitAvailability { get; set; } 
    }

public class VisitTypes
    {
        public int ID { get; set; }
        public string Description { get; set; }
        public int? SiteID { get; set; }
        public virtual Sites Site { get; set; }
        public int? StatusID { get; set; }
        public virtual Status Status { get; set; }
        public virtual ICollection<Appointments> Appointment { get; set; }
        public virtual ICollection<VisitReasons> VisitReason { get; set; }
        public virtual ICollection<Providers> Provider { get; set; }
    }

控制器:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Assign([Bind(Include = "ID,UserId,VisitTypeID")] Providers providers)
        {

            if (ModelState.IsValid)
            {
                db.Providers.Add(providers);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ManageMessageId? message;
            message = ManageMessageId.NotAssign;
            return RedirectToAction("Index", new { Message = message });//providers
        }

查看:

<div class="form-group">
            @Html.LabelFor(model => model.UserID, "User ID" ,htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.UserID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", @Value = ViewBag.UserId } })
                @Html.ValidationMessageFor(model => model.UserID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.VisitTypeID, "Type of provider", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.VisitTypeID, null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.VisitTypeID, "", new { @class = "text-danger" })
            </div>
        </div>

我的预期结果是 ModelState.IsValid 为 true

将光标指向 ModelState 对象,我敢肯定,它会向您显示确切的错误。