@Html.ValidationMessage adding prepending field. to key

173 views Asked by At

I am trying to use @Html.ValidationMessage like the following:

@Html.TextBox(Model.Key, "", attr)
@Html.ValidationMessage(Model.Key)

However, my validation message isn't displaying.

If I print my Model.Key to the screen, I get "FirstName", however if I look at the source that is created for the above code, it creates it like this:

<input name="FirstName" class="form__control" data-msg-maxlength="This field is limited to 200 characters" data-rule-maxlength="200" data-rule-required="true" id="FirstName" placeholder="Please enter your first name" type="text" value="">
<span class="field-validation-valid" data-valmsg-for="field.FirstName" data-valmsg-replace="true"></span>

Does anyone know why it is prepending the field. to that validation message span or how I can get rid of it?

I have tried typing it in manually:

@Html.ValidationMessage("FirstName")

And this still seems to prepend the field.

Update

Could it be something to do with me using @Html.EditorFor(_ => field) to call a partial that has the textbox and validation message? - If I put the ValidationMessage next to the EditorFor then it works properly (but is out of position)

1

There are 1 answers

1
hitesh panwar On

just do like this

public class A
    {
        public int id { get; set; }
        [Required(ErrorMessage = "Please Enter Name")]
        public string name { get; set; }
}