Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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
Jquery 基于实体框架的MVC跨域验证_Jquery_Asp.net Mvc_Validation - Fatal编程技术网

Jquery 基于实体框架的MVC跨域验证

Jquery 基于实体框架的MVC跨域验证,jquery,asp.net-mvc,validation,Jquery,Asp.net Mvc,Validation,我试图在MVC5中完成一些跨域验证。具体来说,如果选中复选框,则需要自由形式响应区域,这通常是可选的。服务器端似乎工作正常,但我无法让客户端验证正常工作 这里是我的RequiredIfAttribute类和验证器 public class RequiredIfAttribute : ValidationAttribute{ public string DependentUpon { get; set; } public string CallerValue { get; set; } publ

我试图在MVC5中完成一些跨域验证。具体来说,如果选中复选框,则需要自由形式响应区域,这通常是可选的。服务器端似乎工作正常,但我无法让客户端验证正常工作

这里是我的RequiredIfAttribute类和验证器

public class RequiredIfAttribute : ValidationAttribute{
public string DependentUpon { get; set; }
public string CallerValue { get; set; }

public RequiredIfAttribute(string dependentUpon)
{
    this.DependentUpon = dependentUpon;
    //this.Value = null;
}

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
    object instance = validationContext.ObjectInstance;
    Type type = instance.GetType();
    var property = type.GetProperty(DependentUpon);
    var targetValue = property.GetValue(instance);

    if (value == null)
        CallerValue = "";
    else
        CallerValue = value.ToString();

    if(Convert.ToBoolean(targetValue) == true && String.IsNullOrWhiteSpace(CallerValue))
        return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));

    return ValidationResult.Success;
}}

public class RequiredIfValidator : DataAnnotationsModelValidator<RequiredIfAttribute>{
public RequiredIfValidator(ModelMetadata metadata, ControllerContext context, RequiredIfAttribute attribute)
    : base(metadata, context, attribute)
{ }

public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
    var rule = new ModelClientValidationRule
    {
        ValidationType = "requiredifvalidation",
        ErrorMessage = Attribute.FormatErrorMessage(Metadata.PropertyName),
    };

    rule.ValidationParameters.Add("dependentupon", Attribute.DependentUpon);

    return new[] { rule };
}}
标记到我的元数据

    [StringLength(1000)]
    [DataType(DataType.MultilineText)]
    [Display(Name = "Investigator Notes")]
    [RequiredIf("NotToBeProcessed")]
    public string InvestigatorNotes { get; set; }
以及一些来自较旧帖子的JS,以尝试将验证传播到客户端

jQuery.validator.addMethod("requiredifvalidation", function (value, element, params) {
if (this.optional(element)) {
    return true;
}

var dependentUponControl = $("#" + params.dependentUpon);
if (dependentUponControl == null) {
    return false;
}

var dependentUponValue = dependentUponControl[0].value;
return dependentUponValue == value;});

function testConditionEqual(element, params) {
/* Find control for other property */
var dependentUponControl = $("#" + params.dependentUpon);
if (dependentUponControl == null) {
    return false;
}

var dependentUponValue;
if (dependentUponControl[0].type == "checkbox") {
    dependentUponValue = (dependentUponControl[0].checked) ? "True" : "False";
} else {
    dependentUponValue = dependentUponControl[0].value;
}

return dependentUponValue == params.comparand;}
当填写表单并重新加载页面时,服务器端验证将应用,并且不会传递要保存的模型,但“我的表单”页面从不更新在“我的表单”中显示验证

@Html.ValidationMessageFor(model => model.InvestigatorNotes, "", new { @class = "text-danger" })

区域

我以前使用过RequiredIf属性。效果很好。它有服务器端和客户端代码,使用方法与您的完全相同:

public class Category 
{     
    [RequiredIf("IsActive", true)]     
    public string Code { get; set; }
    public bool IsActive { get; set; } 
}

令人惊叹的我尝试了它,但我得到了一个错误:“0x800a138f-JavaScript运行时错误:无法获取未定义或空引用的属性'toString'”我所做的只是复制代码,然后更改元数据以使用新类,当我提交表单时,我得到了错误。我通过更改控件.attr('checked').toString()修复了该错误;要控制.is(“:checked”).toString(),但验证同样不会显示在页面上。。。我的表单提交并被控制器的服务器端验证退回。我也解决了这个问题,但遗憾的是,仍然无法使用该解决方案。返回$.validator.methods[rule].call(this,value,element,ruleparam);引发错误,因为未定义$.validator。有什么想法吗?那么。。经过一点研究,我意识到MVC开箱即用,不会将用于验证的脚本文件添加到布局页面中。。也就是说,我的验证器从未定义过!我已经使用不同的解决方案工作了一周,这可能是没有任何解决方案有效的原因。您考虑过使用
[RequiredIfTrue]
验证属性吗?我考虑过,但我在使用entity framework时遇到了问题,在线上说它仍处于测试阶段。在这两者之间,我想我会选择自己的解决方案。
public class Category 
{     
    [RequiredIf("IsActive", true)]     
    public string Code { get; set; }
    public bool IsActive { get; set; } 
}