我正在研究MVC 2应用程序,我遇到了问题。我的代码如下所示:
public ActionResult Users()
{
try
{
var model = new List<User>
{
new User
{
Name = "Test",
UserID = 1,
Salary = 1000m
}
};
return View(model);
}
catch (Exception ex)
{
//Log exception
return View("ErrorPage");
}
}
[HttpPost]
public ActionResult Users(IEnumerable<User> users)
{
try
{
if (ModelState.IsValid)
{
return RedirectToAction("Index");
}
return View(users);
}
catch (Exception ex)
{
//Log exception
return View("ErrorPage");
}
}
和类用户
public class User
{
[Required]
public string Name{ get; set; }
[Required]
public int UserID{ get; set; }
[Required]
public decimal Salary{ get; set; }
}
我想创建自定义属性(DataAnnototation)来验证发布表单时IEnumerable用户的工资总和是否小于10000?我可以在这里做,因为我的模型是List?
答案 0 :(得分:1)
数据注释无法在集合中运行。
相反,您需要在操作中手动验证。
然后,您可以拨打ModelState.AddModelError()
。