Click here to Skip to main content
12,949,816 members (79,464 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
How to save checkbox values in database.

Below is my view code.

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

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


Below is My controller.

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



My model is


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
[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 29-Mar-12 0:19am
Updated 29-Mar-12 1:25am
v7
Comments
Peace ON 29-Mar-12 6:52am
   
<pre> tag added.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

[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);
            }
  Permalink  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,749
CHill60 3,380
Maciej Los 2,953
Jochen Arndt 1,935
ppolymorphe 1,820


Advertise | Privacy | Mobile
Web01 | 2.8.170524.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2017
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