如何验证 MVC2 表单中的多选?

发布于 2024-08-23 02:47:16 字数 863 浏览 9 评论 0 原文

这似乎是一个非常基本的场景,但我认为它没有一个美好的结局。

我有一个简单的项目类:

public class Project 
{

    [Required(ErrorMessage = "Project title is required")]
    [DisplayName("Project Title")]
    public string Title { get; set; }

    [DisplayName("Related Categories")]
    public Category Categories { get; set; }

}

我想确保至少选择一个相关类别。如何使用 Html.EnableClientValidation() 和模型中的装饰器在视图中验证这一点?如果这不可能,那么后备措施是什么?

同样令人沮丧,并且可能验证的一个障碍是我不能这样做......

<%= Html.ListBoxFor(m => m.Project.Categories,
                new SelectList(Model.Categories, "Id", "Name"))%>

因为这将尝试将 Project.Categories 表单值(字符串数组)与应该是 Category 类型的内容相关联,但不会'不能(我收到错误“从类型‘System.String’到类型的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换”)。因此,我必须将表单名称更改为 m.Categories 之类的名称,从而与 Product 类分离,从而与我想要用来装饰它的任何验证逻辑分离。

哇,对我来说这太疯狂了。我们无法使用 MVC2 装饰器验证简单的多选列表?

This seems like a really basic scenario, but I think it doesn't have a happy ending.

I have a simple project class:

public class Project 
{

    [Required(ErrorMessage = "Project title is required")]
    [DisplayName("Project Title")]
    public string Title { get; set; }

    [DisplayName("Related Categories")]
    public Category Categories { get; set; }

}

I want to ensure at least one related Category is selected. How can I validate this in the view, using Html.EnableClientValidation(), and decorators in the model? If this isn't possible, what is the fallback?

Equally frustrating, and probably an hindrance to validation is that I can't do...

<%= Html.ListBoxFor(m => m.Project.Categories,
                new SelectList(Model.Categories, "Id", "Name"))%>

...because this will attempt to associate the Project.Categories form value (a string array) to what should be a Category type, but won't be able to (I get an error "The parameter conversion from type 'System.String' to type failed because no type converter can convert between these types"). Therefore, I have to change the form name to something like m.Categories, thus disassociating from the Product class, and therefore any validation logic I would like to decorate it with.

Wow, to me this is crazy. We can't validate a simple multiselect list, using MVC2 decorators?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半山落雨半山空 2024-08-30 02:47:16

我将尝试给出答案,因为这就是我现在正在做的事情:

在我的视图模型中,我输入:

    [Required(ErrorMessage = "A category is required")]
    public IEnumerable<Category> Categories { get; set; }

验证将在 UI 中进行。但是,模型状态在操作上仍然没有显示为无效...因为绑定命名不完全匹配,所以这种方法当然不是很可靠,但我认为这是可以解决的。

此外,这还引出了一个关于可能使用 1:1 Action:ViewModel 关系的有趣观点。通过这样做,您可以在视图模型中设置视图特定的验证。这与依赖验证实体不同,后者是更广泛的模型验证的一部分,也是 MVC 2 最终版本的方向。

相关性:

MVC 2 RC 2 中的模型验证:http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html

1:1 视图模型:操作方法:http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/06/29/how-we-do-mvc-view-models.aspx

I'm going to put in my attempt at an answer, since this is what I'm now doing:

In my view model, I put:

    [Required(ErrorMessage = "A category is required")]
    public IEnumerable<Category> Categories { get; set; }

And the validation will occur in the UI. However, the modelstate still doesn't show up as invalid on the action... because the binding naming isn't matching up exactly, so this approach certainly isn't very solid as-is, but I think that can be worked out.

Also, this leads to an interesting point about possibly using 1:1 Action:ViewModel relationship. By doing so, you can set view specific validation in your View Model. This is different than relying on validating entities, which would be part of broader model validation, which is the direction MVC 2 is going for final release.

Of relevance:

Model validation in MVC 2 RC 2: http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html

1:1 View Model:Action approach: http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/06/29/how-we-do-mvc-view-models.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文