ErocM
ErocM

Reputation: 4662

How do I handle multi line AddModelError Errors?

I'm trying to check for multiple errors on my form. Here is the code I have:

  var hasErrors = false;
  var sb = new StringBuilder();

  if (string.IsNullOrEmpty(creditCard.CardNumber))
  {
    hasErrors = true;
    sb.AppendLine("Credit card number is required.");
    //ModelState.AddModelError("PaymentAmount", "Credit card number is required.");
  }

  if (string.IsNullOrEmpty(creditCard.ExpirationDateMonth) || string.IsNullOrEmpty(creditCard.ExpirationDateYear))
  {
    hasErrors = true;
    // ModelState.AddModelError("PaymentAmount", "Expiration date is required.");
    sb.AppendLine("Expiration date is required.");
  }

  if (string.IsNullOrEmpty(creditCard.NameOnCard))
  {
    hasErrors = true;
    // ModelState.AddModelError("PaymentAmount", "Name is required.");
    sb.AppendLine("Name is required.");
  }

  decimal amt = 0;
  creditCard.PaymentAmount = creditCard.PaymentAmount.Replace("$", string.Empty);
  if (!decimal.TryParse(creditCard.PaymentAmount, out amt))
  {
    hasErrors = true;
    //ModelState.AddModelError("PaymentAmount","Amount is invalid.");
    sb.AppendLine("Amount is invalid.");
  }

  if (hasErrors)
  {
    ModelState.AddModelError("PaymentAmount", sb.ToString().Replace(Environment.NewLine,"<br>"));
    return View("CreditCard", creditCard);
  }

I'm trying to get AddModelError to display in multiple lines but I'm not having any luck. It's displaying the <br> as text on the screen instead of rending a break.

I had it where the error was being submitted individually but you'd have to submit the form multiple times before you got the errors on screen. That's why the AddModelError is commented out in each line.

Is there a way to display multiple lines on the AddModelError or is there a better way to handle this?

Thanks for the help!

Upvotes: 1

Views: 1520

Answers (1)

Isma
Isma

Reputation: 15209

You should call ModelState.AddModelError for each of the errors you have in your controller, IMHO, it is not a good practice to mix your validation logic with the way things are rendered in the user interface. In fact, the MVC pattern is all about separating the three concerns, the model (data), the controller (logic, such as validation) and the views (the user interface).

So I would do something like this:

if (string.IsNullOrEmpty(creditCard.CardNumber))
{
      ModelState.AddModelError("PaymentAmount", "Credit card number is required.");
}

if (string.IsNullOrEmpty(creditCard.ExpirationDateMonth) || string.IsNullOrEmpty(creditCard.ExpirationDateYear))
{        
    ModelState.AddModelError("PaymentAmount", "Expiration date is required.");
}

if (string.IsNullOrEmpty(creditCard.NameOnCard))
{
    ModelState.AddModelError("PaymentAmount", "Name is required.");
}

[…]

Then in your view, you can use the following HTML helper to render each error in a list:

If you are using ASP.NET Core:

<div asp-validation-summary="ValidationSummary.ModelOnly"></div>

If you are using the previous versions of ASP.NET MVC:

@Html.ValidationSummary()

This will generate HTML that you can style using CSS.

See here for more info if you are using asp.net core or here for an example if you are using the previous version of ASP.NET MVC.

If you want to display the errors in a different way you can access the errors directly in your view or even better, roll your own helper, see the answers to this question: How do I get the collection of Model State Errors in ASP.NET MVC?

Upvotes: 3

Related Questions