Search code examples
asp.net-mvcrazorattributeshtml-helper

MVC Required attribute not being picked up


In my model I have the property with the [Required] attribute

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

In my view I have the ValidationMessageFor() html helper

<div class="form-group">
    @Html.LabelFor(model => model.Surname, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Surname, "", new { @class = "text-danger" })
    </div>
</div>

However it does not say that the field is required when I go to submit. What am I doing wrong here?


Solution

  • You should add "Jquery.Validate" to validate input datas. But if you do not have a custome error message,required attribute of input tag is simplier.