sitefinity Validation data view

∥☆過路亽.° 提交于 2021-01-24 10:38:07

问题


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; }

回答1:


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

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

})
</script>



回答2:


You can achieve the desired result, you need to wrap your code

@Html.TextBoxFor(x => x.FirstName, new { @class = "form-control", @id = "txtFirstName" })
     @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" }) 

in a div with a class "error-exist" or what ever name you like

also there is a need to add some minor Css mentioned as below:

.error-exist label
{
color:red !important;
}

.error-exist input[type="text"]
{
border:1px solid red !important;
}

add this in your style.css



来源:https://stackoverflow.com/questions/65672858/sitefinity-validation-data-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!