Asp.net mvc MVC 3自定义验证器未被调用
我正在尝试验证我的dropdownlist项是否已存在于数据库中。它应该将Model.IsValid设置为false,但它始终传递true,看起来验证程序只是没有被调用 下拉列表代码:Asp.net mvc MVC 3自定义验证器未被调用,asp.net-mvc,Asp.net Mvc,我正在尝试验证我的dropdownlist项是否已存在于数据库中。它应该将Model.IsValid设置为false,但它始终传递true,看起来验证程序只是没有被调用 下拉列表代码: </td> </tr> <tr> <td> @Html.Label("VendorId ") </td> <td> @Html.DropDow
</td>
</tr>
<tr>
<td> @Html.Label("VendorId ") </td>
<td> @Html.DropDownListFor(x => x.id, new SelectList(Model.validatingVendors, "VendorKey", "VerndorID", Model.id)) @Html.ValidationMessageFor(x=>x.validatingVendors.FirstOrDefault(s=>s.VendorKey == x.id).VerndorID)</td>
</tr>
我的模范班
public class VelidationVendorViewModel
{
public int VendorKey { get; set; }
[Required]
[Vendor(ErrorMessage = "Vendor Allerede eksister")]
public string VerndorID { get; set; }
public int LatestSalesCWeek { get; set; }
public bool Active { get; set; }
}
我的parentViewModel
public class ParentViewModel
{
public List<FactRefIdFileFormatIdViewModel> model { get; set; }
public RefrenceDataViewModel refModel { get; set; }
public List<MetaFileFormatsViewModel> metaModel { get; set; }
public int id { get; set; }
public VelidationVendorViewModel vendorId { get; set; }
public IEnumerable<VelidationVendorViewModel> validatingVendors { get; set; }
}
在我的viewmodel上是错误的,因为我必须将自定义验证移动到我的ParentViewModel内的id,因此它看起来如下所示:
public List<FactRefIdFileFormatIdViewModel> model { get; set; }
public RefrenceDataViewModel refModel { get; set; }
public List<MetaFileFormatsViewModel> metaModel { get; set; }
[Required]
[Vendor(ErrorMessage = "Vendor Allerede eksister")]
public int id { get; set; }
public VelidationVendorViewModel vendorId { get; set; }
public IEnumerable<VelidationVendorViewModel> validatingVendors { get; set; }
public List<FactRefIdFileFormatIdViewModel> model { get; set; }
public RefrenceDataViewModel refModel { get; set; }
public List<MetaFileFormatsViewModel> metaModel { get; set; }
[Required]
[Vendor(ErrorMessage = "Vendor Allerede eksister")]
public int id { get; set; }
public VelidationVendorViewModel vendorId { get; set; }
public IEnumerable<VelidationVendorViewModel> validatingVendors { get; set; }
返回(manager.VendorAllreadyExcists(stringValue));
如果确实存在,则返回TRUE,这是您想要的吗?如果不存在,是否要返回TRUE?(从事实上假设,当名称为“无效”时,您返回TRUE-不可针对DB进行测试)刚刚解决了这个问题
[Required]
[Vendor(ErrorMessage = "Vendor Allerede eksister")]
public string VerndorID { get; set; }
[Required]
[Vendor(ErrorMessage = "Vendor Allerede eksister")]
public string VerndorID { get; set; }
在我的viewmodel上是错误的,因为我必须将自定义验证移动到我的ParentViewModel内的id,因此它看起来如下所示:
public List<FactRefIdFileFormatIdViewModel> model { get; set; }
public RefrenceDataViewModel refModel { get; set; }
public List<MetaFileFormatsViewModel> metaModel { get; set; }
[Required]
[Vendor(ErrorMessage = "Vendor Allerede eksister")]
public int id { get; set; }
public VelidationVendorViewModel vendorId { get; set; }
public IEnumerable<VelidationVendorViewModel> validatingVendors { get; set; }
public List<FactRefIdFileFormatIdViewModel> model { get; set; }
public RefrenceDataViewModel refModel { get; set; }
public List<MetaFileFormatsViewModel> metaModel { get; set; }
[Required]
[Vendor(ErrorMessage = "Vendor Allerede eksister")]
public int id { get; set; }
public VelidationVendorViewModel vendorId { get; set; }
public IEnumerable<VelidationVendorViewModel> validatingVendors { get; set; }
公共列表模型{get;set;}
公共引用DataViewModel参考模型{get;set;}
公共列表元模型{get;set;}
[必需]
[Vendor(ErrorMessage=“Vendor Allerede eksister”)]
公共int id{get;set;}
公共VelidationVendorViewModel vendorId{get;set;}
public IEnumerable validatingVendors{get;set;}
最后但并非最不重要的一点是,我的验证必须调用ID,而不是列表中特定项目的vendorId,如下所示:
@Html.ValidationMessageFor(x=>x.Id)
@Html.ValidationMessageFor(x=>x.Id)刚刚解决了我的问题,但还不能通过我的回答。。。我的属性不匹配