我应该通过部分呈现表单的可选部分吗?
作为学习 .NET 的练习,我将一些简单的表单转移到 MVC 中,但遇到了一个问题。该表格是一个包含选项部分的多部分表格。例如,第 0 部分是静态的,包含用户名、真实姓名、电子邮件地址等信息。之后是一个带有多个选项的单选按钮。如果您单击第一个单选按钮,它将显示第 1 部分。如果您选择第二个单选按钮,它将显示第 2 部分,依此类推。
在 WebForms 中,这没什么大不了的,因为我只是在回发时进行验证,并说如果 Radio1.Selected 验证这一点,如果 Radio2.Selected 验证这一点,等等。所以现在我有了一个带有 [Required] 成员的强类型视图,这显然行不通——我不能要求那些并不总是需要的成员。
话虽如此,这是解决问题的正确方法吗:
- 在我的强类型视图模型类中创建属于第 0 节的成员。
- 在我的视图模型类中创建对每个部分的强类型类的引用。
- 创建局部视图,然后在主视图中渲染它们。
- 根据选择的单选按钮,渲染适当的局部视图。
- 像往常一样验证模型......这有望级联到部分模型。
这是否有意义,或者方法是错误的?
As an exercise in learning .NET, I'm moving some simple forms over into MVC, and have run into an issue. The form in question is a multi-part form that has option sections. For example, Section 0 is static and contains information like username, real name, email address. After that is a radio button with several options. If you click the first radio, it displays Section 1. If you choose the second, it displays Section 2, and so on.
In WebForms this was no biggy, as I just validated on postback and said if Radio1.Selected validate this, if Radio2.Selected validate that, etc. So now I've got a strongly-typed view with [Required] members, which obviously isn't going to work - I can't require members that aren't always going to be required.
With that said, is this the correct approach to the problem:
- Create the members that belong in Section 0 in my strongly-typed view model class.
- Create references to each partial's strongly-typed class in my view model class.
- Create the partial views and then render them in the main view.
- Depending on which radio button is selected, render the appropriate partial view.
- Validate the model like usual...which hopefully will cascade to the partial models.
Does this make sense, or is the approach wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是需要条件验证的典型场景,即如果设置了某个值,则验证是否需要其他值。使用静态数据注释来实现这一点(这些注释是在编译时烘焙的简单属性)由于其声明性性质,很快就会变成一场噩梦。好吧,您始终可以滚动自己的自定义验证属性,但属性的问题是您必须将属性名称指定为字符串,因为它们需要在编译时知道。
这就是我使用 FluentValidation.NET 的原因之一。不仅验证规则与视图模型是分开的,而且它与 ASP 集成得非常好.NET MVC,但处理这样的场景会非常容易。您可以拥有一个包含子部分的所有属性的子视图模型,然后根据其验证器内主视图模型上给定属性的值有条件地包含它。
That's a typical scenario where you need conditional validation, i.e. if some value is set then validate that some other value is required. Achieving this with static data annotations which are simple attributes baked at compile time can quickly turn into a nightmare due to their declarative nature. Well, you could always roll your own custom validation attributes but the problem with attributes is that you will have to specify property names as strings as they need to be known at compile time.
That's one of the reasons why I use FluentValidation.NET. Not only that validation rules are separate from the view models and that it integrates really nicely with ASP.NET MVC but handling scenarios like this would be very easy. You could have a sub-view model containing all the properties of the sub-section and then conditionally include it based on the value of a given property on the main view model inside its validator.