gpt4 book ai didi

c# - RequiredIf 没有触发客户端验证

转载 作者:行者123 更新时间:2023-11-30 12:26:46 24 4
gpt4 key购买 nike

我正在使用 JaroslawWaliszkoExpressiveAnnotations .当我通过 ModelState.IsValid 检查服务器端时,它工作正常。但它没有显示客户端验证消息。我不知道缺少什么东西。我也添加了 jquery 文件。这是我应用 RequiredIf 的属性:

// When Role = Assistant Professor(which has id = 3), 
// His/ Her head's Id should be selected as ParentID.

[RequiredIf("RoleID == 3", ErrorMessage = "Select Head.")]
public Nullable<int> ParentID { get; set; }

呈现的 HTML 如下:

<select class="form-control ng-pristine ng-valid" 
data-val="true"
data-val-number="The field ParentID must be a number."
data-val-requiredif="Select Head."
data-val-requiredif-allowempty="false"
data-val-requiredif-constsmap="{}"
data-val-requiredif-expression="RoleID == 3"
data-val-requiredif-fieldsmap="{"RoleID":"numeric"}"
id="ParentID"
name="ParentID"
ng-model="DTO.ParentID"
ng-options="obj.Value as obj.Text for obj in headList">

<option selected="selected" value="" class="">--select--</option>
<option value="0">Mr. Kevin Thomas</option>
<option value="1">Ms. Lisa Brown</option>
<option value="2">Mr. Sail Kapoor</option>
</select>

我已经实现的事情:

  1. 已安装的包:Install-Package ExpressiveAnnotations
  2. Global.asax 中添加了以下代码

    DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof (RequiredIfAttribute), typeof (RequiredIfValidator));
    DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof (AssertThatAttribute), typeof (AssertThatValidator));
  3. jquery 验证文件下方的 bundle 中添加了 expressive.annotations.validate.js,并在指定页面上添加了 bundle 。

最佳答案

我觉得还不错。控制台输出是否有任何错误?

你可以试试这个对我有用:

public class Model
{
public IEnumerable<SelectListItem> Supervisors
{
get
{
return new[]
{
new SelectListItem {Text = "Mr. Kevin Thomas", Value = "0"},
new SelectListItem {Text = "Ms. Lisa Brown", Value = "1"},
new SelectListItem {Text = "Mr. Sail Kapoor", Value = "2"}
};
}
}

[RequiredIf("RoleID == 3", ErrorMessage = "Select Head.")]
public int? ParentID { get; set; }
...

和 View :

@Html.DropDownListFor(model => model.ParentID, Model.Supervisors, "--select--")
@Html.ValidationMessageFor(model => model.ParentID)

此外,除非您已经看过它,否则请查看 sample project在那里你可以找到一些类似的案例。

更新:

Look here for few troubleshooting steps .

关于c# - RequiredIf 没有触发客户端验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27540131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com