Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to save checkbox values in database.

Below is my view code.

C#
@foreach (var item in Model)
{
    @Html.CheckBox("statecheck", (IEnumerable<SelectListItem>)ViewData["StatesList"])
    @Html.DisplayFor(modelItem => item.state_name)
}

HTML
<input class="ASPbutton" type="submit" value="submit"/>


Below is My controller.

C#
public ActionResult States()
{
    ViewData["StatesList"] = new SelectList(am.FindUpcomingStates().ToList(), "state_id", "state_Name");
    return View();
}



My model is


C#
public IQueryable<state> FindUpcomingStates()
{
    //return from state in Adm.states orderby state.state_name select state;
}


After clicking submit button checked item state_id will be saved into database.

I wrote like below in Controller, but i got true or false values, i want state_id
C#
[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult States(string _stateName, char[] statecheck, FormCollection formvalues)
        {    
           statecheck = Request.Form["statecheck"].ToArray();
           ViewData["StatesList"] = new SelectList(am.FindUpcomingStates222().ToList(), "state_id", "state_Name", _stateName);
            }

Thanks,
Posted
Updated 29-Mar-12 1:25am
v7
Comments
[no name] 29-Mar-12 6:52am    
<pre> tag added.

hi

i done this in vb.net windows application,the code here below just edit as ur try to run

----------------INSERT----------------------------
Dim a1,a2,a3 as boolean
Dim a4 as string
a1 = CheckBox1.CheckState
a2= CheckBox2.CheckState
a3= CheckBox3.CheckState
If a1 = True Then
a1 = True
Else
a1 = False
End If
If a2 = True Then
a2 = True
Else
a2 = False
End If
If a3 = True Then
a3 = True
Else
a3 = False
End If
a4 = a1 & "|" & a2 & "|" & a3
‘ Now you can store a4 value as string to database easily

‘ it store values in db like 0|1|0
‘that means true state is 0 false state 1

-----------------REtreive-----------------
‘Now You retrieve database values either as datatable or dataset I use ‘datadable object dt here is the code below
Dim str as string

s = dt.Rows(0).Item("Activities")
Dim a() As String
Dim com, j As Integer

Dim arrs As String
a = s7.Split("|")
For j = 0 To a.Length - 1
arrs = a(j)

If j = com And arrs = "True" Then
CheckBox1.CheckState = True
ElseIf j= com+1 And arrs = "True" Then
CheckBox2.CheckState = True
ElseIf j = com + 2 And arrs = "True" Then
CheckBox3.CheckState = True
End If

If j=com And arrs = "False" Then
CheckBox1.CheckState = False
ElseIf j=com+1 And arrs = "False" Then
CheckBox2.CheckState = False
ElseIf j=com+2 And arrs = "False" Then
CheckBox3.CheckState = False
End If
Next
 
Share this answer
 
SQL
[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult States(string _stateName, char[] statecheck, FormCollection formvalues)
        {
           statecheck = Request.Form["statecheck"].ToArray();
           ViewData["StatesList"] = new SelectList(am.FindUpcomingStates222().ToList(), "state_id", "state_Name", _stateName);
            }
 
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