Click here to Skip to main content
15,935,259 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I wants to add Checkboxes in my application..MVC-3(Aspx) Asp.Net(C#)
I asked to add checkboxes and and save the values of that checkboxes which are selected..

But i even don't know how to add checkbox to the database.

can anyone help me for that?

Thanks in advance..

There are two ways to achieve your functionality.

1. You can build your model with string property and add <input type="checkbox"></input> controls with html stuff you need and then render it from model in view.
public class checkboxes
  public string checks{get; set;}

Then you need pass this model from controller to view and access it like following.
<%: Model.checkboxes %>

In that way it will render as many as checkboxes as you want.

2. Second method is related to first but with some difference. In this method you need to pass model with only some parameters of checkboxes not the html code. And from those parameters we are building checkbox control on the view.

public class checkboxes
  public List<string> checksname{get; set;}

Then in view you just loop through names and create checkboxes.
<% foreach(string checkname in Model.checkboxes)
{ %>
<%: Html.CheckBox(checkname, false) %>
<% } %>
Share this answer
in the view add:
<input style="margin: 0px;padding: 0px;width: 15px;height: 15px; border: 0px; background: none;" type="checkbox" name="RememberMe" id="RememberMe" value="true" />

and in the controller you can get the vale of checkbox as:
public ActionResult LogOn(bool? rememberMe)
                bool remember = Convert.ToBoolean(rememberMe);

and now the bool type variable you can save in database , for this create a column of type bit
Share this answer
disha desani 15-Mar-12 6:29am    
i tried your code but in the view it doesn't display the name with checkbox.
it display just a checkbox..
member60 15-Mar-12 6:36am    
hey ! add lable for it or you can use
CheckboxFor in mvc

or try:
<input type="checkbox" id="RememberMe" name="RememberMe" value="true" />
<label for="RememberMe">
Remember me?</label>
disha desani 15-Mar-12 7:01am    
ooh so if i have to save the data of that checkbox(means here "RememberMe") in database.. then i have to save the name property(name="RememberMe") of the checkbox?
member60 15-Mar-12 7:07am    
i have given the name for that checkbox as RememberMe if you want you can change and save
disha desani 15-Mar-12 7:15am    
okey :) one more question..
i don't wants to save the true or false.. but i wants to save the data of that checkbox which are checked=true..
in your eg. if checkbox of "RememberMe" is checked=true/selected then i wants to save the data "RememberMe" in database..??
Add column (like checked) to your database table.
Take data type of that field as "Bit".
if checkbox is cheked then pass "True"
if unchecked pass "False"
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