asp.net mvc 相关的可空实体
我在使用 Model.IsValid 时遇到一些问题。我正在尝试创建一个与此类似的模型...
class Article {
int Id {get; set;}
string Title {get; set;}
Category Category {get; set;}
}
class Category {
int Id {get; set;}
string Name {get; set;}
IList<Articles> Articles {get; set;}
}
我希望文章类别可以为空。但是,当类别为空时,Model.IsValid 始终为 false。
任何帮助表示赞赏。
I'm having some problems with Model.IsValid. I am trying to create a model similar to this...
class Article {
int Id {get; set;}
string Title {get; set;}
Category Category {get; set;}
}
class Category {
int Id {get; set;}
string Name {get; set;}
IList<Articles> Articles {get; set;}
}
I would like that the Article Category can be null. However Model.IsValid is always false when the Category is null.
Any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯...您确定 Model.IsValid 由于类别为空而为假吗?我之所以这么问,是因为我在域对象的 Category 属性中没有看到任何“必需”的数据注释。或者,如果在绑定类别时确实失败,那么您可能需要创建一个自定义模型绑定器,这就是我认为(更可能是猜测)您发生错误的地方。
Hmmm ... are you sure the Model.IsValid is false because of category being null? I am asking this since I don't see any 'required' data annotations in the Category property in your domain object. OR if it is indeed failing when binding on the Category then you might need to create a custom model binder which is where I think - more likely a guess - your error is happening.