<small id='vugRl'></small><noframes id='vugRl'>

    <legend id='vugRl'><style id='vugRl'><dir id='vugRl'><q id='vugRl'></q></dir></style></legend>
      <bdo id='vugRl'></bdo><ul id='vugRl'></ul>
    <tfoot id='vugRl'></tfoot>
    1. <i id='vugRl'><tr id='vugRl'><dt id='vugRl'><q id='vugRl'><span id='vugRl'><b id='vugRl'><form id='vugRl'><ins id='vugRl'></ins><ul id='vugRl'></ul><sub id='vugRl'></sub></form><legend id='vugRl'></legend><bdo id='vugRl'><pre id='vugRl'><center id='vugRl'></center></pre></bdo></b><th id='vugRl'></th></span></q></dt></tr></i><div id='vugRl'><tfoot id='vugRl'></tfoot><dl id='vugRl'><fieldset id='vugRl'></fieldset></dl></div>

    2. MVC.NET Core 中的条件验证(RequiredIf)

      时间:2023-07-12
      <legend id='9aEJT'><style id='9aEJT'><dir id='9aEJT'><q id='9aEJT'></q></dir></style></legend>

        1. <i id='9aEJT'><tr id='9aEJT'><dt id='9aEJT'><q id='9aEJT'><span id='9aEJT'><b id='9aEJT'><form id='9aEJT'><ins id='9aEJT'></ins><ul id='9aEJT'></ul><sub id='9aEJT'></sub></form><legend id='9aEJT'></legend><bdo id='9aEJT'><pre id='9aEJT'><center id='9aEJT'></center></pre></bdo></b><th id='9aEJT'></th></span></q></dt></tr></i><div id='9aEJT'><tfoot id='9aEJT'></tfoot><dl id='9aEJT'><fieldset id='9aEJT'></fieldset></dl></div>

            <small id='9aEJT'></small><noframes id='9aEJT'>

            • <tfoot id='9aEJT'></tfoot>

                <tbody id='9aEJT'></tbody>

              • <bdo id='9aEJT'></bdo><ul id='9aEJT'></ul>
                本文介绍了MVC.NET Core 中的条件验证(RequiredIf)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                I am trying to conditionally validate the field within the MVC.NET Core. I have two radio buttons. If I select Yes (for the Ownership) I want to make a field below required (Activity dropdown)

                但是,无论我如何努力,要验证的值始终来自 Activity 字段,而不是来自 Ownership 字段(NA"而不是Yes")

                However, no matter how hard I try, the value to be validated always comes from the Activity field, not from the Ownership field ("NA" instead of "Yes")

                谁能告诉我我做错了什么

                Can somebody please tell me what I am doing wrong

                视图 (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;
                    }
                }
                

                推荐答案

                找到答案

                改变了

                if (proprtyvalue.ToString() == DesiredValue.ToString() && value == null)
                

                if (proprtyvalue.ToString() == DesiredValue.ToString() && value.ToString() == "N/A")
                

                这篇关于MVC.NET Core 中的条件验证(RequiredIf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:是否可以从 wwwroot 文件夹外部提供静态文件? 下一篇:将 html 帮助程序迁移到 ASP.NET Core

                相关文章

                <small id='fYpcq'></small><noframes id='fYpcq'>

              • <legend id='fYpcq'><style id='fYpcq'><dir id='fYpcq'><q id='fYpcq'></q></dir></style></legend>
                    <bdo id='fYpcq'></bdo><ul id='fYpcq'></ul>

                1. <i id='fYpcq'><tr id='fYpcq'><dt id='fYpcq'><q id='fYpcq'><span id='fYpcq'><b id='fYpcq'><form id='fYpcq'><ins id='fYpcq'></ins><ul id='fYpcq'></ul><sub id='fYpcq'></sub></form><legend id='fYpcq'></legend><bdo id='fYpcq'><pre id='fYpcq'><center id='fYpcq'></center></pre></bdo></b><th id='fYpcq'></th></span></q></dt></tr></i><div id='fYpcq'><tfoot id='fYpcq'></tfoot><dl id='fYpcq'><fieldset id='fYpcq'></fieldset></dl></div>

                    <tfoot id='fYpcq'></tfoot>