1

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 ?

StanK
  • 4,750
  • 2
  • 22
  • 46
user3087881
  • 340
  • 4
  • 14
  • 1
    Please post the relevant code – Maess Dec 10 '13 at 18:16
  • I can't. I'm new to stackOverflow and have no reputation (which mean i can't post pictures) – user3087881 Dec 10 '13 at 18:40
  • Copy and paste it, then use the code tag to format it as code. – Maess Dec 10 '13 at 18:41
  • there's a bit of mess in the end, but i did my best. – user3087881 Dec 10 '13 at 19:03
  • What are the actual error messages you are getting? If you drill into the exception in your debugger, you should find exactly what the two EntityValidationErrors are saying - which should help you figure out what the problem is. – StanK Dec 10 '13 at 20:23
  • it sais i have a missing City and Country prop inside the Address class of the Contact. this make no sence, since i can see those object loaded with details before i use unitOfWork.Save(); I'm starting to think maybe there is something wrong wijth my contex or migration. – user3087881 Dec 13 '13 at 16:07

1 Answers1

1

Just get the errors from modelstate.

using Linq:

var allErrors = ModelState.Values.SelectMany(v => v.Errors);

another option:

public static List<string> GetErrorListFromModelState
                                              (ModelStateDictionary modelState)
{
      var query = from state in modelState.Values
                  from error in state.Errors
                  select error.ErrorMessage;

      var errorList = query.ToList();
      return errorList;
}

resource:

How to get all Errors from ASP.Net MVC modelState?

Community
  • 1
  • 1
Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90