I have a form which asks user to select Yes or No based on some questions.Intitally none of the option will be selected.If user clicks save button without selecting the option validation should be shown
<div>
<label for="IsSourceCodeCollected" class=" control-label">
Question 1
</label>
</div>
<div>
<label style="width:100px">
@Html.RadioButtonFor(m => m.StudentProjectApprovalTrainer.IsSourceCodeCollected, true, new { @class = "minimal sourceCodeCollected" })
YES
</label>
<label style="width:100px">
@Html.RadioButtonFor(m => m.StudentProjectApprovalTrainer.IsSourceCodeCollected, false, new { @class = "minimal sourceCodeCollected" })
NO
</label>
</div>
@Html.ValidationMessageFor(m => m.StudentProjectApprovalTrainer.IsSourceCodeCollected)
<button type="submit" id="btnSubmit" class="btn btn-info pull-left ">
<i class="fa fa-save"></i>
Save
</button>
Model
[Required(ErrorMessage = "Please select this")]
public bool IsSourceCodeCollected { get; set; }
Javascript
$(document).on("click", "#btnSubmit", function () {
var form = $("#frmAdd");
if (form.valid()) {
alert("hai");
};
return false;
});
When I clicks the save button without selecting Yes or No alert is shown which means form is valid.
How can i solve this scenario ??
IsSourceCodeCollectedofStudentProjectApprovalTrainerandIsSourceCodeCollectedis aboolwhich means that one of the radio buttons will be always be selected when the view is rendered because its initial value is eithertrueorfalse(and cannot be anything else)bool? IsSourceCodeCollected) which means if the user does not select one, the validation message will be displayed when the submit button is clicked.bool tobool?solved the issue.Can you post the answer so that it would help other newbies like me :).