0
votes

In ASP.NET using Entity Framework I'm trying to add a new entry to a database named Provider. The view returns a default value UserID and a category as a foreign key from another table. When I debug the controller Post method has an if (ModelState.IsValid) that keeps returning false. Can't understand why probably I don't fully understand the ModelState and how it works.

When I override the if(ModelState.IsValid) to if(true) it successfully adds the new entry to the database with all the fields properly set.

When I verify the locals it presents the following: (I chose to place a pic in as a link due to not having enough reputation to use the image format) 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

Model:

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; }
    }

Controller:

        [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
        }

View:

<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>

My expected result is for the ModelState.IsValid to be true.

1
show us a JSON that you POST to this methodvasily.sib
also, inspect ModelState object. It should contains a validation errors that occures.vasily.sib
ModelState.Values contains error which error you found?Lalji Dhameliya
instead of user direct Entities you should change it to viewModel in your main class thats properties not should contains null value (ID, UserID, VisitTypes, VisitAvailability) so that it returns trueCarlos Cocom
When I override the if(ModelState.IsValid) to if(true) it successfully adds the new entry to the database with all the fields properly set.FelicianoEJ

1 Answers

0
votes

Direct your cursor into ModelState object, i'm sure, it will show you the exact error.