本文介绍了验证在MVC 4下拉列表中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这是我的视图模型。 // 供应商信息 public string VendorName {获得; set ; } [显示(名称= 供应商)] [必填( ErrorMessage = 请选择供应商)] public Guid VendorId { get ; set ; } // 供应商列表 public IEnumerable< IpNetworkViewModel> GetVendor(Guid Cid) { var SQry = 来自 l db.Vendors 其中 l.CustomerId == Cid orderby l.VendorName 选择 new {l.VendorId ,l.VendorName}; List< IpNetworkViewModel> SList = new 列表< IpNetworkViewModel>(); SList.Add( new IpNetworkViewModel {VendorId = Guid.Empty,VendorName = - 选择供应商 - }); foreach ( var p in SQry) { SList.Add( new IpNetworkViewModel {VendorId = p.VendorId,VendorName = p.VendorName}) ; } 返回 SList; } 这是我的查看 < div class = all-div > @ Html.LabelFor(model => model.VendorId) < / div > < div class = all-div > < div class = drop_down > @ Html.DropDownListFor(model => model.VendorId,new SelectList(new ConsultantsHub.Areas.ChAdmin.ViewModel.IpNetworkViewModel()。GetVendor(Model.CustomerId),VendorId,VendorName)) @ Html.ValidationMessageFor(model => model .VendorId) < / div > ; < / div > 任何人,请帮助我如何为上面的下拉列表进行必要的字段验证。解决方案 This is my "view model".//Vendor info public string VendorName { get; set; } [Display(Name = "Vendor")] [Required(ErrorMessage = "Please select a Vendor")] public Guid VendorId { get; set; } //Vendor list public IEnumerable<IpNetworkViewModel> GetVendor(Guid Cid) { var SQry = from l in db.Vendors where l.CustomerId == Cid orderby l.VendorName select new { l.VendorId, l.VendorName }; List<IpNetworkViewModel> SList = new List<IpNetworkViewModel>(); SList.Add(new IpNetworkViewModel { VendorId = Guid.Empty, VendorName = "-Select Vendor-" }); foreach (var p in SQry) { SList.Add(new IpNetworkViewModel { VendorId = p.VendorId, VendorName = p.VendorName }); } return SList; }This is my "View"<div class="all-div"> @Html.LabelFor(model => model.VendorId) </div> <div class="all-div"> <div class="drop_down"> @Html.DropDownListFor(model => model.VendorId, new SelectList(new ConsultantsHub.Areas.ChAdmin.ViewModel.IpNetworkViewModel().GetVendor(Model.CustomerId), "VendorId", "VendorName")) @Html.ValidationMessageFor(model => model.VendorId) </div> </div>Any one, please help me how to do required field validation for above drop down list. 解决方案 这篇关于验证在MVC 4下拉列表中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-12 12:53