可參考以下文章:
http://blog.darkthread.net/post-2011-09-02-jquery-validate-ubnotrusive-checkbox-required.aspx
http://blog.degree.no/2012/03/validation-of-required-checkbox-in-asp-net-mvc/
http://itmeze.com/2010/12/06/checkbox-has-to-be-checked-with-unobtrusive-jquery-validation-and-asp-net-mvc-3
我的做法
單選就 radiobutton
複選就 checkbox 搭配 ValidationAttribute 使用
但我不使用 boolearn model 搭配 List
至於單選的 CheckBox 呢???嘿嘿嘿
SERVER 跟 CLIENT 都新增
【SERVER 擴充】
public class CheckBoxRequiredAttribute : ValidationAttribute, IClientValidatable
{
public CheckBoxRequiredAttribute()
{
}
public override bool IsValid(object value)
{
bool result = false;
if (value == null)
return result;
List
if (r != null && r.Count > 0)
result = true;
return result;
}
public IEnumerable
ModelMetadata metadata,
ControllerContext context)
{
var rule = new ModelClientValidationRule()
{
ValidationType = "checkboxrequired",
ErrorMessage = String.Format("{0} 欄位是必要項。", metadata.DisplayName)
};
yield return rule;
}
}
【CLIENT 擴充】
$.validator.unobtrusive.adapters.add("checkboxrequired", function (options) {
沒有留言:
張貼留言