troubleshooting Question

Model entity validation error - remove required field

Avatar of Intelli-Seeker
Intelli-SeekerFlag for United States of America asked on
DatabasesASP.NET.NET MVCSQL
5 Comments1 Solution124 ViewsLast Modified:
I am using ASP.NET with an MVC 5 project and entity framework. I want to remove the required attribute that is created implicitly based on the SQL database field. I keep getting entity validation errors on the EMailAddress2 field in the form. I tried these two things among others.

1)  I added @{ Html.EnableClientValidation(false);} to the code in the create view for that div, but that did not resolve the issue.
2) I added DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; to the global.asax.

Create view code:

<h2>Create</h2>

@{ Html.EnableClientValidation(false);}
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>user</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CustomerName, "", new { @class = "text-danger" })
            </div>
        </div>

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

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

        <div class="form-group">
            @{ Html.EnableClientValidation(false);}
            @Html.LabelFor(model => model.EMailAddress2, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EMailAddress2, new { htmlAttributes = new { @class = "form-control" } })
                @*@Html.ValidationMessageFor(model => model.EMailAddress2, "", new { @class = "text-danger" })*@
            </div>
        </div>

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

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

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

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

        <div class="form-group">
            @Html.LabelFor(model => model.Enrolled, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.Enrolled)
                    @Html.ValidationMessageFor(model => model.Enrolled, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.UnEnrollPending, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.UnEnrollPending)
                    @Html.ValidationMessageFor(model => model.UnEnrollPending, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

Controller Code for Post:

       public ActionResult Create([Bind(Include = "IDNumber,CustomerName,AccountNumber,EMailAddress1,EMailAddress2,Password,ChangedBy,DateEnrolled,DateUnEnrolled,Enrolled,UnEnrollPending")] user user)
        {
            //ModelState.Remove("EMailAddress2");
            //ModelState["EMailAddress2"].Errors.Clear();
            if (ModelState.IsValid)
            {
                db.users.Add(user);
                try
                { 
                db.SaveChanges();
                }
                catch (DbEntityValidationException e)
                {
                    Console.WriteLine(e);
                }
                return RedirectToAction("Index");
            }

            return View(user);
        }

Model Class

namespace CustomerEnrollment.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class user
    {
        public long IDNumber { get; set; }
        public string CustomerName { get; set; }
        public string AccountNumber { get; set; }
        public string EMailAddress1 { get; set; }
        public string EMailAddress2 { get; set; }
        public string Password { get; set; }
        public string ChangedBy { get; set; }
        public System.DateTime DateEnrolled { get; set; }
        public Nullable<System.DateTime> DateUnEnrolled { get; set; }
        public bool Enrolled { get; set; }
        public Nullable<bool> UnEnrollPending { get; set; }
    }
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros