validationasp.net mvc中的下拉列表

//in controller ViewBag.Categories = categoryRepository.GetAllCategories().ToList(); //in view @Html.DropDownList("Cat", new SelectList(ViewBag.Categories,"ID", "CategoryName")) 

我怎么能这样做,默认情况下它说:“ – select类别 – ”

并validation选中的东西(客户端和模型)

谢谢

我只是不能相信有人仍然在ASP.NET MVC 3中使用ViewData / ViewBag,而不是强types的视图和视图模型:

 public class MyViewModel { [Required] public string CategoryId { get; set; } public IEnumerable<Category> Categories { get; set; } } 

并在你的控制器中:

 public class HomeController: Controller { public ActionResult Index() { var model = new MyViewModel { Categories = Repository.GetCategories() } return View(model); } [HttpPost] public ActionResult Index(MyViewModel model) { if (!ModelState.IsValid) { // there was a validation error => // rebind categories and redisplay view model.Categories = Repository.GetCategories(); return View(model); } // At this stage the model is OK => do something with the selected category return RedirectToAction("Success"); } } 

然后在你的强types视图中:

 @Html.DropDownListFor( x => x.CategoryId, new SelectList(Model.Categories, "ID", "CategoryName"), "-- Please select a category --" ) @Html.ValidationMessageFor(x => x.CategoryId) 

此外,如果你想客户端validation不要忘记引用必要的脚本:

 <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script> 

有3个参数的重载。 Html.DropdownList(name, selectList, optionLabel) 更新 :在下面的代码片段中有一个错字。

 @Html.DropDownList("Cat", new SelectList(ViewBag.Categories,"ID", "CategoryName"), "-Select Category-") 

供validation人使用

 @Html.ValidationMessage("Cat") 

对于MVC5中的ListBox / DropDown – 我发现这个为我工作sofar:

在模型中:

 [Required(ErrorMessage = "- Select item -")] public List<string> SelectedItem { get; set; } public List<SelectListItem> AvailableItemsList { get; set; } 

在视图中:

 @Html.ListBoxFor(model => model.SelectedItem, Model.AvailableItemsList) @Html.ValidationMessageFor(model => model.SelectedItem, "", new { @class = "text-danger" }) 

使用Dataannotation和ViewBag提交下拉列表validation的MVC 4示例(less一行代码)

楷模:

 namespace Project.Models { public class EmployeeReferral : Person { public int EmployeeReferralId { get; set; } //Company District //List [Required(ErrorMessage = "Required.")] [Display(Name = "Employee District:")] public int? DistrictId { get; set; } public virtual District District { get; set; } } namespace Project.Models { public class District { public int? DistrictId { get; set; } [Display(Name = "Employee District:")] public string DistrictName { get; set; } } } 

EmployeeReferral控制器:

 namespace Project.Controllers { public class EmployeeReferralController : Controller { private ProjDbContext db = new ProjDbContext(); // // GET: /EmployeeReferral/ public ActionResult Index() { return View(); } public ActionResult Create() { ViewBag.Districts = db.Districts; return View(); } 

视图:

 <td> <div class="editor-label"> @Html.LabelFor(model => model.DistrictId, "District") </div> </td> <td> <div class="editor-field"> @*@Html.DropDownList("DistrictId", "----Select ---")*@ @Html.DropDownListFor(model => model.DistrictId, new SelectList(ViewBag.Districts, "DistrictId", "DistrictName"), "--- Select ---") @Html.ValidationMessageFor(model => model.DistrictId) </div> </td> 

为什么我们不能使用ViewBag填充可以使用批注进行validation的下拉列表。 这是更less的代码行。