【问题标题】:Problem with RequiredIf constructor overloading .net core 3.1RequiredIf 构造函数重载.net core 3.1的问题
【发布时间】:2021-08-11 10:43:48
【问题描述】:

我正在尝试在 .net 核心中为我的一个项目使用 requiredif 验证器。 我有 2 个 requiredif 属性的构造函数

public RequiredIfAttribute(string s1,string s2,string errormsg=""){}

还有

public RequiredIfAttribute(string s1,string s2,string s3,string errormsg=""){}

当使用以下代码时,对于以下两种情况,只有第一个具有 3 个参数的构造函数被调用 [RequiredIf("test","test","test","test")], [RequiredIf("test","test","test")]

有谁知道在RequiredIf中传递4个参数时如何用4个参数实际命中构造函数

代码

public class model
   {
     [RequiredIf("test","test","test",ErrorMessage="test")]
     public string content{get:set;}
     [RequiredIf("test","test",ErrorMessage="test")]
      public string version{get;set;}
   } 


public classs RequiredIfAttribute:ValidationAttribute
{
public RequiredIfAttribute(string s1,string s2,string errormsg=""){}

public RequiredIfAttribute(string s1,string s2,string s3,string errormsg=""){}
}
     

【问题讨论】:

  • 对我来说这听起来不太可能。编译器不只是忽略属性参数。请问你能发一个minimal reproducible example吗?
  • 我已经编辑了代码。问题似乎是由于可选参数。如何在不删除可选参数的情况下解决此问题?
  • 您还没有提供minimal reproducible example。如果我无法重现问题,我将无法为您提供帮助。

标签: c# validation .net-core asp.net-core-webapi


【解决方案1】:

请参阅下面的示例代码,它对我有用。

using System.ComponentModel.DataAnnotations;

namespace WebApplication1.Util
{
    public class RequiredIfAttribute : ValidationAttribute
    {
        private readonly string _s1;
        private readonly string _s2;
        private readonly string _s3;
        private readonly string _errormsg;
        public RequiredIfAttribute(string s1, string s2, string errormsg = "") {
            _s1 = s1;
            _s2 = s2;
            _errormsg = errormsg;
        }
        public RequiredIfAttribute(string s1, string s2, string s3, string errormsg = "") {
            _s1 = s1;
            _s2 = s2;
            _s3 = s3;
            _errormsg = errormsg;
        }

        public override bool IsValid(object value)
        {
            if (_s3 == null)
            {
                var inputValue = (string)value;
                if (_s1 == "test" && _s2 == "test")
                {
                    return false;
                }
                return true;
            }
            else {
                if (_s1 == "test" && _s2 == "test" && _s3 == "test")
                {
                    return false;
                }
                return true;
            }
            
        }

        public override string FormatErrorMessage(string name)
        {
            return _errormsg;
        }
    }
}

我的测试模型

using System.ComponentModel.DataAnnotations;
using WebApplication1.Util;

namespace WebApplication1.Models
{
    public class User
    {
        [RequiredIf("test","test","errorMsg")]
        public string user_desc { get; set; }
        [Required(ErrorMessage = "age is necessary")]
        public string age { get; set; }
        [RequiredIf("test", "test", "test", "errorMsg for three param")]
        public string content { get; set; }
    }
}

我的控制器:

public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create([Bind("user_desc,age,content")] User user)
        {
            if (ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }
            return View(user);
        }

我的看法

@model WebApplication1.Models.User
@{

}

<h4>User</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="user_desc" class="control-label"></label>
                <input asp-for="user_desc" class="form-control" />
                <span asp-validation-for="user_desc" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="age" class="control-label"></label>
                <input asp-for="age" class="form-control" />
                <span asp-validation-for="age" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="content" class="control-label"></label>
                <input asp-for="content" class="form-control" />
                <span asp-validation-for="content" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

这是测试结果。

【讨论】:

    猜你喜欢
    • 2020-07-30
    • 1970-01-01
    • 2020-07-13
    • 2021-01-28
    • 2020-05-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    相关资源
    最近更新 更多