0

I'm trying to get a input that will show error messages in two places, a asterisk beside the input field and then a more detailed error message at the bottom of the section. Currently I have

    <div>
        <div>Html.DropDownListFor(x => x.selectMode, new SelectList(Model.modes))</div>
        @Html.ValidationMessageFor(x => x.selectMode,"*",new {@class = " text-danger"})
    </div>

Which displays the asterisk when the input is invalid just fine, but the problem is that it overrides the other message that's suppose to be at the bottom so it doesn't show.

    <div>
         @Html.ValidationMessageFor(x => x.selectMode,"Select a valid mode",new {@class = " text-danger"})
    </div>

What should I be doing differently to make both error messages display?

EDIT: The model itself has only one error per field, I am just trying to make it display twice in two different places using @Html.ValidationMessageFor() or something similar.

KevinLamb
  • 634
  • 8
  • 18
Mylies
  • 396
  • 4
  • 18
  • Is the `@class = " text-danger` a typo while making this question or is that in the actual code? (It's missing a quote in both) – KevinLamb Aug 01 '19 at 03:17
  • Also, does [this question](https://stackoverflow.com/questions/8223197/how-to-display-multiple-validation-errors-with-html-validationmessagefor) help you out? – KevinLamb Aug 01 '19 at 03:25
  • I think you should always display an asterisk beside require field. It's better for design UI. – Tomato32 Aug 01 '19 at 03:34
  • 1
    Possible duplicate of [How to display multiple validation errors with @Html.ValidationMessageFor?](https://stackoverflow.com/questions/8223197/how-to-display-multiple-validation-errors-with-html-validationmessagefor) – Hoshani Aug 01 '19 at 05:55
  • @KevinLamb Yes that was a typo in the question I didnt notice apologizes, and unfortunately that question is similar to what Im trying to achieve but Im trying to get the same error message to display using two different ValidationMessageFor instead of getting two errors to display in the same place. – Mylies Aug 02 '19 at 16:15
  • @Hoshani Its a similar question but Im trying to get the same message to display twice in different places and unfortunately that question is about overloading ValidationMessageFor to display two error messages together – Mylies Aug 02 '19 at 16:16
  • 1
    @DavidWolak I cannot reproduce the issue you describe here. Please create an [mcve] – NineBerry Aug 02 '19 at 16:35
  • @DavidWolak, I'm in the same boat as NineBerry. Possibly show your model and controller? – KevinLamb Aug 02 '19 at 17:28

3 Answers3

1

Model

public class TestModel
{
    public string TestProperty { get; set; }
}

Controller Action

public IActionResult Test()
{
    ModelState.AddModelError("TestProperty", "TestErrorMessage");
    var m = new TestModel();
    return View(m);
}

View Test.cshtml

@model TestModel
Text @Html.TextBoxFor(m => m.TestProperty)

@*Show a star as validation error*@
<div>
    @Html.ValidationMessageFor(m => m.TestProperty, "*", new { @class = " text-danger" })
</div>

@*Show a custom validation error message*@
<div>
    @Html.ValidationMessageFor(m => m.TestProperty, "Another custom error message", new { @class = " text-danger" })
</div>


@*Show the validation error message from the validation state. Use null for message to show message from validation state*@
<div>
    @Html.ValidationMessageFor(m => m.TestProperty, null, new { @class = " text-danger" })
</div>
NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • This is almost exactly what I need! However wheres there is more then one property being validated it once again goes back to displaying only the first type of message displayed. – Mylies Aug 05 '19 at 19:00
0

Turns out the issue was not that it wasn't displaying both messages, but that the messages can either only be custom or give to it from the model. In my case I was trying to do both with a custom message(the asteriks) and one from the model(This field is required).

When I made one custom the other one was displaying but as "" which made it look invisible but it was there the entire time.

Mylies
  • 396
  • 4
  • 18
  • What you say is not correct. You can output a custom message at one place in the template and output the message from the model at a different place in the template. Create a [mcve] and we can find out what goes wrong – NineBerry Aug 02 '19 at 19:42
0

ValidationSummary() Method can show all error message.

<div>
         @Html.ValidationSummary(false, "", new { @class = "text-danger" })
</div>
Minal Chauhan
  • 6,025
  • 8
  • 21
  • 41
KMT
  • 1