I'm using MVC 4 with EntityFramework 5.
In this example I'm getting two EntityValidationErros after i try to save a new "Contact" object with simple form parameters.
one of the Contact attributes is "Address", which is been filled on the same view (with the other Contact's attributes.
when I submit the form, i get a brand new Contact object with a full Address atribute which matches the form parameters. Yet, when i try to save it, i get EntityValidationError (2 to be exact..) of missing parameters inside my Contact's Address.
My view:
<div class="editor-label">
@Html.LabelFor(model => model.Address.City)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.City)
@Html.ValidationMessageFor(model => model.Address.City)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address.Street)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.Street)
@Html.ValidationMessageFor(model => model.Address.Street)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address.State_province)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.State_province)
@Html.ValidationMessageFor(model => model.Address.State_province)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address.Zip_postalCode)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.Zip_postalCode)
@Html.ValidationMessageFor(model => model.Address.Zip_postalCode)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address.Country)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.Country)
@Html.ValidationMessageFor(model => model.Address.Country)
</div>
<br />
<p>
<input type="submit" value="Save" />
</p>
Create Action inside ContactCOntroller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Contact contact)
{
try
{
if (ModelState.IsValid)
{
TempData["contact"] = contact;
unitOfWork.currentClient.Contacts.Add(contact);
unitOfWork.Save();
return View("Index");
}
return View(contact);
}
catch (Exception ex)
{
return View(contact);
}
}
Any Idea ?