【发布时间】:2013-06-24 14:25:16
【问题描述】:
不确定我是否正确填充了下拉列表,但在验证下拉列表中的值时遇到问题。选择一个值后,它仍然显示错误“值 x 无效”。类型是int?据我所知 int 不适用于验证器。
查看型号代码:
[Display(Name = "Category")]
[Required(ErrorMessage = "Category is required.")]
public AWS.DTO.Lookup Category { get; set; }
public IEnumerable<AWS.DTO.Lookup> Categories { get; set; }
控制器代码:
[PageOptions(Title = "Create FMR")]
public ActionResult Create()
{
var model = new FMRRequestViewModel();
model.Categories = new AWS.BL.Lookup().GetFMRCategories();
return View(model);
}
查找类型:
public class Lookup
{
public int? ID { get; set; }
public string Description { get; set; }
}
查看代码:
@Html.DropDownListFor(m => m.Category, new SelectList(Model.Categories, "ID", "Description", -1), "-- Please Select -- ")
提前感谢您的帮助。
【问题讨论】:
-
为什么你的
Lookup类的ID可以为空? -
因为我读到验证器不适用于不可为空的类型,但正如您所见,我完全错了。
标签: asp.net-mvc asp.net-mvc-3 validation