Intelli-Seeker
asked on
Model entity validation error - remove required field
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.EnableClientValidatio n(false);} to the code in the create view for that div, but that did not resolve the issue.
2) I added DataAnnotationsModelValida torProvide r.AddImpli citRequire dAttribute ForValueTy pes = false; to the global.asax.
Create view code:
Controller Code for Post:
Model Class
1) I added @{ Html.EnableClientValidatio
2) I added DataAnnotationsModelValida
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; }
}
}
ASKER
I still want EMailAddress2 to be a field in the form and have the data saved to the database. Will this cause issues with the form?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks - I will post back if this doesn't resolve the issue. I also changed the model to allow NULLs for that field.
ASKER
Thanks. I will try that and post back if it doesn't work. I also changed to database structure to allow NULLs for the EMailAddress2 field.
Please change
Open in new window
to
Open in new window