Click here to Skip to main content
15,846,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi friends I will show you my issue Now I have this model
public class Admin
{
C#
[Key]
        public int ID { get; set; }

C#
[Required, MaxLength(400) ]
       public string LoginName { get; set; }

C#
public bool ISEnroled { get; set; }


}

and this is my controller

C#
public ActionResult Admins( Admin rec, bool ISEnroled)
var dbrec = db.Admins.Find(rec.ID);
 dbrec.LoginName = rec.LoginName;
 dbrec.ISEnroled = ISEnroled;
db.SaveChanges();
       {

and this is the view
<script>
    function submitForm() {
    <pre>  $("#formParentID").submit();
        
    }
</script>


HTML
<form action="@Url.Action("Admins")" id="formParentID" target="_self" method="post" enctype="multipart/form-data">
                            @Html.Hidden("ID", Model.ID)

<div class="blog_form_input">
                                <label>Login Name</label>
                                @Html.TextBox("LoginName", Model.LoginName, new { @class = "name_input" })
                                <span id="errorLoginName" style="display:none"> <label style="color:red">You have to enter login name </label></span>
                            </div>

 <div class="blog_form_input">
                                <label>Enroll</label>
                                @{ string ISEnroled = Model.ISEnroled ? "checked" : "";}
                                <input type="checkbox" name="ISEnroled" id=@string.Format("ISEnroled" + Model.ISEnroled) class="css-checkbox9 all_check" value="@Model.ISEnroled" @ISEnroled />
                                <label for=@string.Format("ISEnroled" + Model.ISEnroled) class="css-label9" checked="checked">&nbsp; </label>
                           </div>


the problem here when I click the checkbox to pass the true value it doesn't pass and
save it as true value in the database I don't know why even it is false value it is not passing to save in the database


what's wrong I have done ?

What I have tried:

I tried to use the code above but I couldn't configure the error
Posted
Updated 22-Mar-18 1:48am
v2

Quote:
HTML
<input type="checkbox" name="ISEnroled" ... value="@Model.ISEnroled" @ISEnroled />

You're setting the value of the checkbox to the current value from the model, which is false.

So even if you check the checkbox, when you submit the form, the value you're submitting is "false".

Use either the CheckBox[^] or CheckBoxFor[^] helpers to render the checkbox:
HTML
<div class="blog_form_input">
    @Html.LabelFor(m => m.LoginName, "Login Name")
    @Html.TextBoxFor(m => m.LoginName, new { @class = "name_input" })
    <span id="errorLoginName" style="display:none"> <label style="color:red">You have to enter login name </label></span>
</div>
<div class="blog_form_input">
    @Html.LabelFor(m => m.ISEnroled, "Enroll")
    @Html.CheckBoxFor(m => m.ISEnroled, new { @class = "css-checkbox9 all_check" })
    <label for="ISEnroled" class="css-label9" checked="checked">  </label>
</div>
 
Share this answer
 
Comments
Sarah Mohammed77 23-Dec-16 3:36am    
Hi Richard I used the code you wrote , but why ckeckbox is not visible when I run the site , I coudn't see the checkbox?
Sarah Mohammed77 23-Dec-16 3:37am    
Hi Richard , why checkbox not visible I couldn't see it after I run the site ?
Sarah Mohammed77 23-Dec-16 3:55am    
thank you for your time and effort Richard , now it works
Member 13613423 18-Aug-18 7:33am    
when i clicked checkbox .it stored '' in the database
We can achieve it by setting hidden field value and get it's value on the form post


@Html.LabelFor(m => m.ISEnroled, "Enroll")

<input type="checkbox" name="ISEnroled" id=@string.Format("ISEnroled" + Model.ISEnroled) class="css-checkbox9 all_check" value="@Model.ISEnroled" @ISEnroled />




and write a script to set hidden fields overwrite value and get this set hidden field value on the form post



$("#ISEnroled").click(function () {
var isChecked = document.getElementById('ISEnroled').checked;
$("#hdnIsEnroll").val(isChecked);
});

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900