Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0 MVC3
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..
Posted 15-Mar-12 0:41am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;}
}</string>
Then in view you just loop through names and create checkboxes.
<% foreach(string checkname in Model.checkboxes)
{ %>
<%: Html.CheckBox(checkname, false) %>
<% } %>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
direction.d at 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 at 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>
direction.d at 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 at 15-Mar-12 7:07am
   
i have given the name for that checkbox as RememberMe if you want you can change and save
direction.d at 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..??
member60 at 16-Mar-12 0:52am
   
for retrieving the data true or false at runtime in controller it is neccessary to set it to true first in view. try it first and then reply me.
runtime if you select the checkbox it returns true otherwise false , but the thing is that first set it to true, it is my personal experience know.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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"
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,142
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 15 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100