Watch this example on YouTube:
Replace
@Html.CheckBoxFor(x => x.MyQuestions)
with
@Html.CheckBox("MyQuestions", Model.MyQuestions ?? false)
Replace
@Html.CheckBoxFor(x => x.MyQuestions)
with
@Html.CheckBox("MyQuestions", Model.MyQuestions ?? false)
1. Validation
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ValidationType = "validatecheckbox",
ErrorMessage = "Fix it!"
};
rule.ValidationParameters.Add("fields", string.Join(",", _fields));
yield return rule;
}
2. JS
$.validator.addMethod('validatecheckbox', function (value, element, params) {
debugger
var isValid = true;
if ($(params)[0].fields[1].val() == "True"){
if ($(params)[0].fields[0][0].checked == false) {
isValid = false;
}
}
});
Replace
@model WebApplication3.Models.EmployeeList
@using (Html.BeginForm(null, null, FormMethod.Post))
{
if (Model.myEmployees != null)
{
foreach (var item in Model.myEmployees)
{
@Html.CheckBoxFor(modelItem => item.IsValid.Value, new { id = item.EmployeeID})
@Html.DisplayFor(modelItem => item.FirstName)
<br />
}
}
}
with
@model WebApplication3.Models.EmployeeList
@using (Html.BeginForm(null, null, FormMethod.Post))
{
if (Model.myEmployees != null)
{
foreach (var item in Model.myEmployees)
{
@Html.CheckBoxFor(modelItem => item.IsValid.Value, new { id = item.EmployeeID, @disabled = "true"})
@Html.DisplayFor(modelItem => item.FirstName)
<br />
}
}
}
To fix it replace
@model WebApplication3.Models.EmployeeList
@using (Html.BeginForm(null, null, FormMethod.Post))
{
if (Model.myEmployees != null)
{
foreach (var item in Model.myEmployees)
{
@Html.CheckBoxFor(modelItem => item.IsValid.GetValueOrDefault, new { id = item.EmployeeID })
@Html.DisplayFor(modelItem => item.FirstName)
}
}
}
with
@model WebApplication3.Models.EmployeeList
@using (Html.BeginForm(null, null, FormMethod.Post))
{
if (Model.myEmployees != null)
{
foreach (var item in Model.myEmployees)
{
@Html.CheckBoxFor(modelItem => item.IsValid.Value, new { id = item.EmployeeID })
@Html.DisplayFor(modelItem => item.FirstName)
}
}
}
to fix it replace
@model WebApplication3.Models.EmployeeList
@using (Html.BeginForm(null, null, FormMethod.Post))
{
if (Model.myEmployees != null)
{
foreach (var item in Model.myEmployees)
{
@Html.CheckBoxFor(modelItem => item.IsValid, new { id = item.EmployeeID })
@Html.DisplayFor(modelItem => item.FirstName)
}
}
}
with
@model WebApplication3.Models.EmployeeList
@using (Html.BeginForm(null, null, FormMethod.Post))
{
if (Model.myEmployees != null)
{
foreach (var item in Model.myEmployees)
{
@Html.CheckBoxFor(modelItem => item.IsValid.Value, new { id = item.EmployeeID })
@Html.DisplayFor(modelItem => item.FirstName)
}
}
}
To fix this error replace (semicolon) in view
@model WebApplication3.Models.EmployeeList;
with
@model WebApplication3.Models.EmployeeList
Declare @Test varchar(100) = 'aaa@aaa';
IF @Test Like '%@%'
BEGIN
print ' contains @'
END
ELSE
BEGIN
print ' doesn''t contain @'
END