0

I'm trying to show page errors so I have written the following code:

    <div class="errors">
        @{
        ViewData.ModelState.Values.SelectMany(v => v.Errors).ToList()
       .ForEach(e => { Html.Raw($"<span>{e.ErrorMessage}</span>"); });
        }
   </div>

The Html.Raw() does not output anything. Razor - HTML.RAW does not output text suggest writing it like @Html.Raw which in my case is not valid

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171

1 Answers1

2

e.ErrorMessage isn't HTML, so you must not use Html.Raw().

Instead, you should use a simple loop:

@foreach(var e in ViewData.ModelState.Values.SelectMany(v => v.Errors)) {
  <span>@e.ErrorMessage</span>
}
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Very nice, it solved the problem. but still I wonder how to write the Html.Raw value (for sake of learning) – Ashkan Mobayen Khiabani Jul 31 '17 at 18:55
  • @AshkanMobayenKhiabani: `Html.Raw()` returns a value, which you must print on the page. You can call the `Write` methods (from the base class) directly from a lambda, but the general answer is to use `@foreach` instead of lambdas. – SLaks Jul 31 '17 at 19:01