我是新的ASP.NETMVC和工作的形式验证.我被一组复选框验证卡住了.基本上,我试图让用户在一组复选框中 Select 至少一个爱好.
下面的模型.您可以看到,我试图实现AtLeastOneCheckedAttribute
,但无法使其正常工作.
public class EmployeeModel2
{
[Required(AllowEmptyStrings = false, ErrorMessage = "Please Provide First Name")]
[StringLength(20, MinimumLength = 5, ErrorMessage = "First Name Should be min 5 and max 20 length")]
public string FirstName { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Please Provide Last Name")]
[StringLength(20, MinimumLength = 5, ErrorMessage = "First Name Should be min 5 and max 20 length")]
public string LastName { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Please Provide Eamil")]
[RegularExpression("^[a-zA-Z0-9_\\.-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$", ErrorMessage = "Please Provide Valid Email")]
public string Email { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Please Provide Age")]
[Range(25, 45, ErrorMessage = "Age Should be min 25 and max 45")]
public int? Age { get; set; }
[Required(ErrorMessage = "Please Provide Gender")]
public string Gender { get; set; }
//[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
[Required(ErrorMessage = "Please enter Date of Birth")]
public DateTime DOB { get; set; }
[DataType(DataType.Password)]
[Required(ErrorMessage = "Please enter password")]
public string Password { get; set; }
[Required(ErrorMessage = "Please enter ConfirmPassword")]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "Password not matching")]
public string ConfirmPassword { get; set; }
public List<HobbyMV> Hobbies { get; set; }
[AtLeastOneChecked(ErrorMessage = "Please select at least one checkbox")]
public int[] selectedHobbies { get; set; }
}
// I tried this code, but it is not working properly
public class AtLeastOneCheckedAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
var instance = value as IEnumerable<HobbyMV>;
if (instance != null)
{
return true;
}
return false;
}
}
视图中的表格.问题是,即使我选中了几个复选框,selectedHobbies
个组复选框仍会提示"请至少 Select 一个复选框".
@using (Html.BeginForm("Validation2", "XForm", FormMethod.Post, htmlAttributes: new
{@class = "form"}))
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(model => model.FirstName)
@Html.TextBoxFor(model => model.FirstName, new { @class = "form-control", title = "Enter your first name" })
@Html.ValidationMessageFor(m => m.FirstName, "", new {@class = "text-danger"})
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName)
@Html.TextBoxFor(model => model.LastName, new { @class = "form-control", title = "Enter your last name" })
@Html.ValidationMessageFor(m => m.LastName, "", new {@class = "text-danger"})
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email)
@Html.TextBoxFor(model => model.Email, new { @class = "form-control", title = "Enter your email address" })
@Html.ValidationMessageFor(m => m.Email, "", new {@class = "text-danger"})
</div>
<div class="form-group">
@Html.LabelFor(model => model.Age)
@Html.TextBoxFor(model => model.Age, new { @class = "form-control", title = "Enter your age" })
@Html.ValidationMessageFor(m => m.Age, "", new {@class = "text-danger"})
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DOB)
@Html.ValidationMessageFor(model => model.DOB, "", new {@class = "text-danger"})
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password, "", new {@class = "text-danger"})
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ConfirmPassword, "", new {@class = "text-danger"})
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ConfirmPassword)
@Html.ValidationMessageFor(model => model.ConfirmPassword, "", new {@class = "text-danger"})
</div>
<div class="form-group" >
<label><b>Gender</b></label>
<div>
@Html.RadioButtonFor(m => m.Gender, "Male")
<label>Male</label>
@Html.RadioButtonFor(m => m.Gender, "Female")
<label> Female </label>
@Html.ValidationMessageFor(m => m.Gender, "", new {@class = "text-danger"})
</div>
</div>
<div class="form-group" >
<label><b>Hobby</b></label><br/>
@foreach (var item in Model.Hobbies)
{
<span style="padding:10px;">
<label><input type="checkbox" value="@item.HobbyId" checked="@item.IsSelected" name="selectedHobbies" id="@item.HobbyId" /> @Html.Raw(@item.Description)</label>
</span>
}
@Html.ValidationMessageFor(m => m.selectedHobbies, "", new {@class = "text-danger"})
</div>
<div class="form-group">
<input type = "submit" value="Submit" class="btn-success" />
</div>
}
如果我为这个爱好 Select 了一些复选框,然后点击提交,它没有通过验证(下面的截图),并且消息"请至少 Select 一个复选框"不会消失.
请帮帮忙.先谢谢你.