【问题标题】:is there a way to call a function in a model to set a value? ASP.NET C#有没有办法调用模型中的函数来设置值? ASP.NET C#
【发布时间】:2021-03-25 14:04:11
【问题描述】:

我需要根据我已经创建的功能更改模型中的值有没有办法做到这一点?

public bool TempBool = !!!!!!!where i function needs be called!!!!!!;
        [Compare(nameof(TempBool), ErrorMessageResourceName = "RegisterModelConsentErrorMessage", ErrorMessageResourceType = typeof(SharedResource))]
        [Display(Name = "RegisterModelConsentDisplayName", ResourceType = typeof(SharedResource))]
        public bool Consent { get; set; }

我要调用的函数如下。

public bool CheckGdpr()
    {
        string str = _configuration["privacy:GDPR"];
        string checkbol = "True";
        if (checkbol == str)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

`公共类RegisterModel { public bool DisableChangePassword { get;放; }

    private readonly IConfiguration _configuration;

    public RegisterModel(IConfiguration configuration)
    {
        _configuration = configuration;
    }
    public bool TempBool => Convert.ToBoolean(_configuration["privacy:GDPR"]);
    [Compare(nameof(TempBool), ErrorMessageResourceName = "RegisterModelConsentErrorMessage", ErrorMessageResourceType = typeof(SharedResource))]
    [Display(Name = "RegisterModelConsentDisplayName", ResourceType = typeof(SharedResource))]
    public bool Consent { get; set; }
}`

但是如果我的项目有一个公共的 Registermodel 抛出方法可能会出错,是否有另一种方法可以将其命名为与类相同?

【问题讨论】:

  • 你试过什么?如果函数在同一个类中,似乎 public bool TempBool = CheckGdpr(); 应该完成这项工作。
  • 你的意思是 public bool TempBool { get;放; } = CheckGdpr(); ?
  • 在这里使用方便的 readonly 属性语法:public bool TempBool { get; set; } => CheckGdpr(); - 如果你希望你的属性有 getter 和 setter,看起来就像直接访问你的 _configuration 这样public bool TempBool { get => _configuration["privacy:GDPR"] == "True"; set => _configuration["privacy:GDPR"] = value.ToString(); }
  • @KingKing 当我使用配置来获取我的全局时,我收到一条错误消息:严重性代码描述项目文件行抑制状态错误 CS0103 当前上下文 DAL C 中不存在名称“配置” :\Users\Isaac\source\repos\elearn2-final_sapce_fixes\Elearn\DAL\Models\Account.cs 124 活动
  • 是否有需要包含的使用配置?

标签: c# asp.net-core


【解决方案1】:

你可以这样使用

public bool TempBool //= !!!!!!!where i function needs be called!!!!!!;
{
    get
    {
        return CheckGdpr();
    }
    set{}   
}
[Compare(nameof(TempBool), ErrorMessageResourceName = "RegisterModelConsentErrorMessage", ErrorMessageResourceType = typeof(SharedResource))]
[Display(Name = "RegisterModelConsentDisplayName", ResourceType = typeof(SharedResource))]
public bool Consent { get; set; }

【讨论】:

    【解决方案2】:

    没有给出与 'RegisterModel.RegisterModel(IConfiguration)' Elearn C:\Users\Isaacr\source\repos\elearn2-final_sapce_fixes\Elearn\Elearn\Controllers\AccountController 所需的形式参数 'configuration' 相对应的参数.cs 113 活动

    对于这个错误,这是因为您创建了一个 RegisterModel 的新实例,但您没有在 AccountController 中将 IConfiguration 传递给它。

    public class AccountController: Controller
    {
        private readonly IConfiguration _configuration;
        public AccountController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        [HttpGet]
        public voidIndex()
        {
            var data = new RegisterModel(_configuration);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 2020-04-12
      相关资源
      最近更新 更多