Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 使用MVC3引导程序在模式中显示自定义验证消息_Asp.net Mvc_Asp.net Mvc 3_C# 4.0_Twitter Bootstrap - Fatal编程技术网

Asp.net mvc 使用MVC3引导程序在模式中显示自定义验证消息

Asp.net mvc 使用MVC3引导程序在模式中显示自定义验证消息,asp.net-mvc,asp.net-mvc-3,c#-4.0,twitter-bootstrap,Asp.net Mvc,Asp.net Mvc 3,C# 4.0,Twitter Bootstrap,我一直在使用引导模式从父页面显示创建表单。表单工作正常,因为它以我希望的方式向数据库添加数据。它显示[必需]批注的错误消息,如果有任何错误,则不允许表单保存数据 问题在于显示自定义验证属性包含的验证消息。我还注意到它没有在我自己的验证属性中触发IsValid函数 以下是我的ValidationAttribute的外观: public class FormulaSyntaxValidationAttribute : ValidationAttribute { public Form

我一直在使用引导模式从父页面显示创建表单。表单工作正常,因为它以我希望的方式向数据库添加数据。它显示[必需]批注的错误消息,如果有任何错误,则不允许表单保存数据

问题在于显示自定义验证属性包含的验证消息。我还注意到它没有在我自己的验证属性中触发IsValid函数

以下是我的ValidationAttribute的外观:

    public class FormulaSyntaxValidationAttribute : ValidationAttribute
{
    public FormulaSyntaxValidationAttribute()
        : base("The given formula is not formatted correctly.")
    { 

    }
    public override bool IsValid(object value)
    {
        return DbManager.ValidateStringIfValidFormula(value.ToString());
    }
}

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FormulaVariableValidationAttribute : ValidationAttribute
{
    public FormulaVariableValidationAttribute()
        : base("The given formula contains other variables instead of `g` or `G` (Gross Salary).")
    {

    }
    public override bool IsValid(object value)
    {
        return DbManager.ValidateFormulaVariables(value.ToString());
    }
}
这是使用已定义属性的模型:

    public partial class Formula
{
    public int FormulaId { get; set; }
    public int DeductionId { get; set; }

    [Required]
    [FormulaSyntaxValidation]
    [FormulaVariableValidation]
    [Display(Name = "Formula")]
    public string FormulaStatement { get; set; }

    [Required]
    [Display(Name = "Minimum Value")]
    public decimal MinimumValue { get; set; }

    [Required]
    [Display(Name = "Maximum Value")]
    public decimal MaximumValue { get; set; }

    public virtual Deduction Deduction { get; set; }
}
我使用在SO中找到的简单javascript来显示验证消息。这是它的样子

    (function ($) {
    $(document).ready(function () {
        $("#addFormulaModal").draggable({
            handle: ".modal-header"
        });

        $('#addFormulaModal').on('shown', function () {
            $('#addFormulaModal').removeData("validator");
            $('#addFormulaModal').removeData("unobtrusiveValidation");
            $.validator.unobtrusive.parse($("#formCreateFormula"));
        });

        $('#addFormulaModal').on('hidden', function () {
            location.reload(true);
        })


        $('#formCreateFormula').submit(function (event) {
            event.preventDefault();
            $(this).find('div.control-group').each(function () {
                if ($(this).find('span.field-validation-error').length == 0) {
                    $(this).removeClass('error');
                }
            });

            if (!$(this).valid()) {
                $(this).find('div.control-group').each(function () {
                    if ($(this).find('span.field-validation-error').length > 0) {
                        $(this).addClass('error');
                    }
                });
            }
        });

        $('#createFormSubmit').each(function () {
            $(this).find('div.control-group').each(function () {
                if ($(this).find('span.field-validation-error').length > 0) {
                    $(this).addClass('error');
                }
            });
        });

    });
})(jQuery);

var page = function () {

    $.validator.setDefaults({
        highlight: function (element) {
            $(element).closest(".control-group").addClass("error");
        },
        unhighlight: function (element) {
            $(element).closest(".control-group").removeClass("error");
        }
    });
}();
提前感谢。

可能的副本