使用数据注释验证集合
刚刚开始使用 ASP.NET MVC,我在将数据注释应用于集合时遇到了一些麻烦 - 我要么缺少一些基本的东西,要么以错误的方式处理整个事情。
我有一个传递给强类型视图的 ViewModel:
public class AddCostingViewModel
{
[Required]
[Range(120,190)]
public int SomeValue {get; set;}
public List<Grade> Grades { get; set; }
public List<int> GradeValues { get; set; }
}
我不知道任一成绩列表中的对象数量将来是否保持不变,因此我无法传递“Grade1、Grade2”等 - 传递像这样的列表并构建我的 cshtml 页面似乎是确保我始终拥有一个包含所有可能成绩并返回正确值的表单的最明显方法。我的 cshtml 看起来像这样:
<tr>
@foreach (var g in Model.Grades)
{
<th>@g.GradeName</th>
}
</tr>
<tr>
@for (int i = 0; i < Model.Grades.Count(); i++)
{
<td>
<div class="editor-field">
@Html.EditorFor(model => model.GradeValues[i])
@Html.ValidationMessageFor(model => model.GradeValues[i])
</div>
</td>
}
</tr>
它可以轻松生成一个表,其中包含用于编辑所有可能的成绩值的字段。但是,我想根据范围验证这些(如上面的 SomeValue 所示),但在列表上方添加数据注释(就像我对 SomeValue 所做的那样)似乎不起作用。这是可能的还是我的方法完全错误?
Just started on ASP.NET MVC, and I'm having some trouble applying datannotations to a collection - I'm either missing something basic, or going about the whole thing in the wrong way.
I have this ViewModel that I pass to a strongly-typed View:
public class AddCostingViewModel
{
[Required]
[Range(120,190)]
public int SomeValue {get; set;}
public List<Grade> Grades { get; set; }
public List<int> GradeValues { get; set; }
}
I have no idea if the number of objects in either grades list will remain constant in the future, so I can't pass in "Grade1, Grade2" etc - passing in a list like this and building up my cshtml page seems like the most obvious way to ensure I always have a form that contains all possible grades, and returns the right values. My cshtml looks something like this:
<tr>
@foreach (var g in Model.Grades)
{
<th>@g.GradeName</th>
}
</tr>
<tr>
@for (int i = 0; i < Model.Grades.Count(); i++)
{
<td>
<div class="editor-field">
@Html.EditorFor(model => model.GradeValues[i])
@Html.ValidationMessageFor(model => model.GradeValues[i])
</div>
</td>
}
</tr>
which handily generates a table containing fields for editing all possible grade values. However, I want to validate these against a range (as with SomeValue above), but adding dataannotations above the list (like I did with SomeValue) doesn't seem to work. Is this possible or is my approach wrong altogether?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来,您的等级值不是简单的 int,它是带有一些特定于域的限制的 int。按照这个逻辑,我将设计新的类来保存等级值
,并且在您的模型中,描述等级值列表的属性将是
并且验证将把 rande 应用于 viewModel 中的每个 GradeValue。并且在视图中,您不必更改任何一行代码。此外,您可以将
GradeValue 类
设计为 隐式可转换为 int,反之亦然。As it seems, your grade value is not simple int, it is int with some domain specific restrictions. Following that logic, I would design new class for holding grade value
And in your model, property that describes list of grade values, would be
And the validation will apply rande to every GradeValue in your viewModel. And in view, you do not have to change any single line of code. Moreover, you could design your
GradeValue class
to be implicitely convertible to int and vice versa, for simpler usage.