Search code examples
twitter-bootstrapbootstrap-4teleriksitefinity

sitefinity Validation data view


I am building this MVC form not using the sitefinity form control. I wanted to add validate to the First Name TextBox. In the model I have [Required] attribute on the property in the view model used by the view. I only get the red text The FirstName field is required. but I wanted the textbox and label to be red as well. Is there another class I need to use for the TextBox or anything with Bootstrap ?

View

@Html.Script(ScriptRef.JQueryValidate, "top", true)

@{
    HtmlHelper.ClientValidationEnabled = true;
    HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
}
     

View

<div class="form-group row">
                        <div class="col-sm-6">
                            @Html.LabelFor(model => model.FirstName)
                            @Html.TextBoxFor(x => x.FirstName, new { @class = "form-control", @id = "txtFirstName" })
                            @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
                        </div>

                        <div class="col-md-6">
                            @Html.LabelFor(model => model.LastName)
                            @Html.TextBoxFor(x => x.LastName, new { @class = "form-control", @id = "txtLastName" })
                            @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
                        </div>
                    </div>

Model

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

Solution

  • Normally, to initialize the jquery validate plugin on the page, you will need to do something like this

    <script>
    $(function() {
      
       $("form").validate();
    
    })
    </script>