ASP.NET MVC 3 validation error not firing when a complex type is used in Entity Framework
Right now I'm working on a form to let user to post content, I want to make use of the WMD editor, so in my Entity Framework model I have a complex type named Content
, it holds HTML and WMD fields.
In the form, there's a textbox for the title, and a WMD editor for Content
, I used the FluentValidation framework, as follow:
public class ArticleValidator : AbstractValidator<Article>
{
public ArticleValidator()
{
RuleFor(x => x.Title).NotEmpty();
RuleFor(x => x.Content.WMD).NotEmpty();
}
}
When I submit this form without entering anything, the client-side validation only catches the Title as invalid. If I enter something in the Title, the form submits (even though the content is empty), then the error is caught on the server-side (empty content), the page is then reloaded with the information I entered, but no error message was displayed.
It seems the complex type I created in the Entity Framework model is causing this problem. I used to have separate properties for ContentHtml
and ContentWMD
and it worked fine.
Is there a workaround to this without having to revert back to where I was?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
You cannot use nested rule definition like this:
You need to have another validator for the Content type:
This being said you should not use EF models inside your views. You should use View Models and define validation rules on your view model: