Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET MVC3
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 1:19am
Edited 29-Mar-12 2:25am
v7
Comments
Peace ON at 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
0 Sergey Alexandrovich Kryukov 565
1 Maciej Los 359
2 Kornfeld Eliyahu Peter 309
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,303
1 DamithSL 4,764
2 Maciej Los 4,306
3 Kornfeld Eliyahu Peter 3,914
4 Sergey Alexandrovich Kryukov 3,538


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 20 Feb 2013
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