麻烦的ModelState.IsValid返回false

时间:2019-04-22 05:28:52

标签: c# asp.net-mvc entity-framework model-binding modelstate

ASP.NET 中使用实体框架 我正在尝试将新条目添加到名为Provider的数据库中。该视图从另一个表返回默认值UserID和类别作为外键。当我调试控制器时,Post方法具有一个if(ModelState.IsValid),该值一直返回false。无法理解为什么我可能不完全了解ModelState及其工作原理。

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

当我验证本地人时,会显示以下内容: (由于没有足够的声誉使用图像格式,我选择将图片作为链接放置) 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

1 个答案:

答案 0 :(得分:0)

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