문제

What is the best way to throw a required validation error using a validation attribute with a dropdownlist that has a default value of 0? If the value is 0, or the default value, I want the attribute to throw the error for my model.

도움이 되었습니까?

해결책

you can provide the option label Docs that will set the selected value to 0 if not specified other wise like @Iridio menttioned in his answer, anotate the view model property with [Required]

public class MyVieWModel
 {
   [Required]
   public int MyValue { get;set;}
   public SelectList MyValues {get;set;}
 }

and in the view

@Html.DropDownListFor(x=>x.MyValue,Model.MyValues,"-- Select --")

다른 팁

Use a RequiredAttribute on your ValueId

something like this

 public class MyVieWModel
 {
   [Required]
   public int MyValue { get;set;}
   public SelectList MyValues {get;set;}
 }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top