1

I have the following models:

class Identity
{
    public string Name { get; set; }
    public string Surname { get; set; }
}

class Person
{
    public Identity Identity { get; set; }
}

Now I want to add a validation model error to the surname in my controller:

[HttpPost]
public ActionResult CreatePerson(Person person)
{
   // some validation stuff
   ModelState.AddModelError("Identity.Surname", "Surname has not been found in BBDD");
                             ^^^^^^^^^^^^^^^^
}

How must I refer to the surname inside the Identity object to show the validation error correctly in my view?

I show the validation error in the view as:

@Html.ValidationMessageFor(model => model.Identity.Surname)

But the error is shown in the general validation summary.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
  • Whats the name of the property in class `Person` - `public Identity WhatName { get; set; }`! –  Oct 23 '14 at 09:23
  • Sorry, It was mispelled, updated. – Daniel Peñalba Oct 23 '14 at 09:30
  • Then `ModelState.AddModelError("Identity.Surname", "...)` is correct - assuming you view has `@Html.ValidationMessageFor(m => m.Identity.Surname)`. –  Oct 23 '14 at 09:35
  • @StephenMuecke: Mmmm I see, it's not really working, I don't know why – Daniel Peñalba Oct 23 '14 at 09:37
  • possible duplicate: http://stackoverflow.com/questions/15504348/modelstate-addmodelerror-no-error-shown – aleha_84 Oct 23 '14 at 09:38
  • Must be another issue. As a side note, if you want client side validation for this property as well you can look at using a `[Remote]` attribute –  Oct 23 '14 at 09:42
  • I assume you are calling `return View(person);` after adding the `ModelState` error –  Oct 23 '14 at 09:43
  • Check in the view the parameter is `true` for `@Html.ValidationSummary(true)` –  Oct 23 '14 at 09:54

1 Answers1

1

you can try to implement Remote attribute to your Surname property. It will allow you to perform validation on client side using ajax to check whatever you want on server. Here is documentation.

class Identity
{
    public string Name { get; set; }
    [Remote("CheckSurname", "Validation")]
    public string Surname { get; set; }
}

public class ValidationController : Controller {

    public JsonResult CheckSurname(string Surname) {

        if(/*your busines logic*/) {
            return Json(true, JsonRequestBehavior.AllowGet);
        }
        else {
            return Json("Your error message here", JsonRequestBehavior.AllowGet);
        } 

    }
}

in web.config you should enable this:

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
aleha_84
  • 8,309
  • 2
  • 38
  • 46
  • Nice suggestion, but **not an answer** to the question –  Oct 23 '14 at 09:55
  • @StephenMuecke just another way to do ops task. He wants error page? He can get it without posting a form. – aleha_84 Oct 23 '14 at 09:57
  • It does not answer OP's question which is _how to add a ModelState error in the controller_ –  Oct 23 '14 at 10:07