1

I have been trying to display validation errors using MVC's inherent validation framework. I am using following code for my Controller:

[HttpPost]
        public ActionResult SaveApplication(ApplicationModel application)
        {
            if (!ModelState.IsValid)
            {                   
                return View("New",application);
            }

            ApplicationBLL.SaveApplication(application);
            return Content(string.Empty);
        }

While my view looks like:

<tr>
        <td>Name</td>
        @Html.ValidationSummary(true)
        <td>@Html.TextBoxFor(m => m.Name)</td>
        <td>@Html.ValidationMessageFor(m => m.Name, "Name is required")</td>
    </tr>

following is how the model class looks like:

public class  ApplicationModel
    {
        public int ApplicationId { get; set; }

        public string ApplicationNumber { get; set; }

        [Required]
        public string Name { get; set; }

        public DateTime EventDate { get; set; }

        public string EventAddress { get; set; }
}

My Model has [Required] validation on the name property and when I put debugger on my controller, it recognizes that the ModelState is not valid and returns back to the view but I do not see any errors on my page. I may be missing something very trivial since this is my first time using MVC's validation framework.

One thing I would like to add is that I am calling Controller with an Ajax Post Can that be contributing to this anomaly?

6
  • Not sure what your asking. You have specified the validation message in the view (as "Name is required") which is what will be displayed. Are you expecting "This is a Test" to be displayed? Commented Sep 17, 2015 at 22:51
  • I removed the Controller Code adding custom error. Thanks for pointing out, it was confusing. My problem is that nothing is showing up on the view when I am expecting that validation failed error should atleast show somewhere on the page Commented Sep 17, 2015 at 22:58
  • 1
    Decorate your property with [Required(ErrorMessage = "Name is required")] public string Name { get; set; } In the view use just @Html.ValidationMessageFor(m => m.Name) and in the controller just if (!ModelState.IsValid) { return View(application); }. If you submit the form with no text in the Name control, you will see the error message (and if client side validation is enabled and the scripts included - the message will be displayed and it wont even submit) Commented Sep 17, 2015 at 23:07
  • Please show us your ApplicationModel viewmodel class. Commented Sep 17, 2015 at 23:17
  • @dai: added the code that you requested Commented Sep 17, 2015 at 23:19

2 Answers 2

1
<td>@Html.ValidationMessageFor(m => m.Name, "Name is required")</td>

This helper method generates a span with attribute below:

data-valmsg-replace="false"

Means that you'll see a static error message below your field, but you'll not be able to change that field with your Error Message defined in Model. Again, you'll see some static error message under your field. if @stephen muecke's solution works, means that your problem wasn't about adding ErrorMessage to wrong place. The only difference I see between your code and @stephen's answer is

return View("New",application);

changed to

return View(application);

This means that maybe you were returning the wrong view from your Action.

Sign up to request clarification or add additional context in comments.

3 Comments

My view's name is "New" so I have to do return View("New",application);. And also it loads the view which I confirmed by putting a break point there. So it is loading the correct view + a model with invalid modelstate. Still I am not seeing the error
Also, I am calling Controller with Ajax. Can it be because of that?
@CoffeeBean which syntax you use for the form post action? jquery $.ajax() or Razor Ajax.BeginForm() ?
0

Try this ,

Add model error in your action

 ModelState.AddModelError("keyName","Error Message");

Use this to display in view

  @Html.ValidationMessage("keyName")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.