【发布时间】: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