PoliDev
PoliDev

Reputation: 1468

Populate checkbox value in ASP.NET MVC

I want to checked default both values. So I used the following code in jQuery..

jQuery:

 $("form #Male").attr('checked', true);
    $("form #Female").attr('checked', true);

After that, the user can select either one of the checkboxes. So I take values from db (model)

 <%: Html.CheckBox("Male", Model.Male.HasValue ? Model.Male.Value : false) %> Male
 <%:Html.CheckBox("Female",Model.Female.HasValue ? Model.Female.Value :false) %> Female

But this is not taking value. In db storing correctly. but in view anyway it is showing both checkboxes are checked default even I change also.

How to recover this problem?

Upvotes: 0

Views: 612

Answers (1)

Dmytro Rudenko
Dmytro Rudenko

Reputation: 2524

Just remove your jQuery code and change default values in your Html.CheckBox() calls:

<%: Html.CheckBox("Male", Model.Male.HasValue ? Model.Male.Value : true) %> Male
<%: Html.CheckBox("Female",Model.Female.HasValue ? Model.Female.Value : true) %> Female

your jQuery code runs every time page loads so doesn't matter which value your checkboxes have - they always will be checked in your code.

Upvotes: 1

Related Questions