gpt4 book ai didi

c# - MVC.NET Core 中的条件验证 (RequiredIf)

转载 作者:行者123 更新时间:2023-11-30 16:39:55 26 4
gpt4 key购买 nike

我正在尝试有条件地验证 MVC.NET Core 中的字段。我有两个单选按钮。如果我选择是(对于所有权),我想在下方创建一个必填字段(事件下拉列表)

但是,无论我多么努力,要验证的值始终来自 Activity 字段,而不是 Ownership 字段(“N\A”而不是“Yes”)

谁能告诉我我做错了什么

View (chtml)

<div class=" form-group">
<div class="bisformdynamiclabel"></div>
<br />
@Html.RadioButtonFor(model => model.BIS232Request.JSONData.OwnershipActivity.Ownership, "Yes", new { id = "OwnershipAnswer_true", onclick = "displayOwnershipFieldsRow(true)" })
<label for="OwnershipAnswer_true">Yes</label>
@Html.RadioButtonFor(model => model.BIS232Request.JSONData.OwnershipActivity.Ownership, "No", new { id = "OwnershipAnswer_false", onclick = "displayOwnershipFieldsRow(false)" })
<label for="OwnershipAnswer_false">No</label>
<span class="alert-danger">
@Html.ValidationMessage("OwnershipAnswer")
</span>
</div>
<div class="row ownershipfieldsrow">
<div class="col-xs-12 col-md-12">
<div class=" form-group">
<div class="bisformdynamiclabel"></div>
<br />
<input style="display:none" class="form-control" type="text" asp-for="BIS232Request.JSONData.OwnershipActivity.Activity" />
<select class="form-control ownershipactivityselect" onchange="$('#BIS232Request_JSONData_OwnershipActivity_Activity').val($(this).val()); ">
<option value="N/A">Please Select</option>
<option value="Manufacturer">Manufacturer</option>
<option value="Distributor">Distributor</option>
<option value="Exporter">Exporter</option>
<option value="Importer">Importer</option>
<option value="Other">Other</option>
</select>
<span asp-validation-for="BIS232Request.JSONData.OwnershipActivity.Activity" class="alert-danger"></span>
<span class="alert-danger">
@Html.ValidationMessage("OwnershipAnswerActivity")
</span>
</div>
</div>

模型

[Required]
public string Ownership { get; set; }
[RequiredIf("Ownership", "OwnershipAnswer_true", "Activity is required if Ownership is selected")]
public string Activity { get; set; }
public class RequiredIfAttribute : ValidationAttribute
{
private String PropertyName { get; set; }
private String ErrorMessage { get; set; }
private Object DesiredValue { get; set; }

public RequiredIfAttribute(String propertyName, Object desiredvalue, String errormessage)
{
this.PropertyName = propertyName;
this.DesiredValue = desiredvalue;
this.ErrorMessage = errormessage;
}

protected override ValidationResult IsValid(object value, ValidationContext context)
{
Object instance = context.ObjectInstance;
Type type = instance.GetType();
Object proprtyvalue = type.GetProperty(PropertyName).GetValue(instance, null);
if (proprtyvalue.ToString() == DesiredValue.ToString() && value == null)
{
return new ValidationResult(ErrorMessage);
}
return ValidationResult.Success;
}
}

最佳答案

基于最初的实现,我建议扩展 RequiredAttribute 而不是 ValidationAttribute - 然后您的默认 ErrorMessage 和其他默认设置按照 [Required] 设置。无论哪种方式,“errormessage”属性都是多余的,因为您已经将其作为 ValidationAttribute 的属性,并且原始代码会为 ErrorMessage 属性生成警告 - 您也可以使用 nameof 用于属性装饰以及使代码中的内容更紧凑:

我的实现稍微更具体一些,因此如果属性是 bool,我可以指示属性是必需的(如果选中复选框):

[AttributeUsage(AttributeTargets.Property)]
public class RequiredIfTrueAttribute : RequiredAttribute
{
private string PropertyName { get; set; }

public RequiredIfTrueAttribute(string propertyName)
{
PropertyName = propertyName;
}

protected override ValidationResult IsValid(object value, ValidationContext context)
{
object instance = context.ObjectInstance;
Type type = instance.GetType();

bool.TryParse(type.GetProperty(PropertyName).GetValue(instance)?.ToString(), out bool propertyValue);

if (propertyValue && string.IsNullOrWhiteSpace(value?.ToString()))
{
return new ValidationResult(ErrorMessage);
}

return ValidationResult.Success;
}
}

示例用法:

public bool IsBusinessProfile { get; set; }

[RequiredIfTrue(nameof(IsBusinessProfile), ErrorMessage = "ABN is required for Business Profiles")]
public string Abn { get; set; }

关于c# - MVC.NET Core 中的条件验证 (RequiredIf),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52321148/

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