如何使用ASP.NET Razor语法应用bootstrap v4表单输入验证类?

时间:2018-01-27 16:14:20

标签: asp.net-mvc razor bootstrap-4

以下代码:

查看:(是有效)的

<div class="form-group">
    @Html.LabelFor(m => m.Telefone, new { @class = "font-weight-bold" })
    @Html.TextBoxFor(m => m.Telefone, new { @class = "form-control is-invalid", @placeholder = "Digite seu telefone" })
    @Html.ValidationMessageFor(m => m.Telefone, "", new { @class = "text-danger" })
</div>

查看:(IS-无效)

for i in range(1, trex.shape[0]-1): 
    for j in range(1,trex.shape[1]-1): 

        if shape[i,j] == 0:    # for 0 degree angles # ONLY GOES THROUGH THIS
            if trex[i,j] < trex[i, j+1] and trex[i,j] < trex[i,j-1]: 
                trex[i,j] = 0

        elif shape[i,j] == 45:    # for 45 degree angles # Never comes down here
            if trex[i,j] < trex[i-1, j+1] and trex[i,j] < trex[i+1,j-1]:
                trex[i,j] = 0

        else    # for 90 degree angles         # Or here
            if trex[i,j] < trex[i-1, j] and trex[i,j] < trex[i+1,j]:
                trex[i,j] = 0

示例:https://getbootstrap.com/docs/4.3/components/forms/#server-side

任何解决方案?

5 个答案:

答案 0 :(得分:3)

使用标签助手的简单解决方案:

        <div class="form-group">
            <input asp-for="Email" class="form-control">
            <div class="invalid-feedback" style="display:block;">
                <span asp-validation-for="Email"></span>
            </div>
        </div>

答案 1 :(得分:1)

//CONFIGURACAO BOOTSTRAP 4 PARA JQUERY VALIDATION PLUGIN
jQuery.validator.setDefaults({
    onfocusout: function (e) {
        this.element(e);
    },
    //onkeyup: false,
    highlight: function (element) {
        jQuery(element).closest('.form-control').addClass('is-invalid');
    },
    unhighlight: function (element) {
        jQuery(element).closest('.form-control').removeClass('is-invalid');
        jQuery(element).closest('.form-control').addClass('is-valid');
    },

    errorElement: 'div',
    errorClass: 'invalid-feedback',
    errorPlacement: function (error, element) {
        if (element.parent('.input-group-prepend').length) {
            $(element).siblings(".invalid-feedback").append(error);
            //error.insertAfter(element.parent());
        } else {
            error.insertAfter(element);
        }
    },
});

答案 2 :(得分:0)

Razor使用jQuery验证。您只需要使用Bootstrap对jq-valid进行仿真:

{
 'connectionStrings':{
   'connectionString': 'your-connection-string'
 }
}

答案 3 :(得分:0)

类名是硬编码的:https://github.com/dotnet/aspnetcore/blob/v3.1.6/src/Mvc/Mvc.ViewFeatures/src/HtmlHelper.cs#L25

唯一的选择是更改CSS。

从源代码构建Bootstrap时,只需将下一个代码添加到SCSS文件中即可:

.input-validation-error {
  @extend .is-invalid;
}

这将为现有的.is-invalid创建别名。

答案 4 :(得分:-2)

一定要包括: &#34; jquery .js jquery.validate .js jquery.validate.unobtrusive .js&#34;