gpt4 book ai didi

c# - 是否可以在 Controller 之外获取 ModelState.IsValid 功能?

转载 作者:太空狗 更新时间:2023-10-29 20:51:36 26 4
gpt4 key购买 nike

<分区>

假设我有一个在 MVC 应用程序中用 [Required] 字段等注释的模型。

在 Controller 中调用 ModelState.IsValid 效果很好,但说我不在 Controller 中,并且想在模型上的应用程序的其他地方运行类似的检查。是否有可能以某种方式以另一种方式调用此功能?

class MyModel{
[Required]
public string Name{get;set;}
}

// Code elsewhere in app that isn't the controller
MyModel model = new MyModel();
//Can I run a modelstate.isvalid type check here on model? Would return false if Name wasn't set

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