如何在数据库中保存复选框选中的值 [英] How to save checkbox checked values in database
本文介绍了如何在数据库中保存复选框选中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在数据库中保存复选框值。
以下是我的查看代码。
< pre lang =c#> @ foreach( var item in Model)
{
@ Html.CheckBox( statecheck,(IEnumerable< SelectListItem>)ViewData [ StatesList])
@ Html.DisplayFor(modelItem = > item.state_name)
}
< ; 输入 class < span class =code-keyword> = ASP按钮 类型 = 提交 value = 提交 / >
以下是我的控制器。
public ActionResult States()
{
ViewData [ StatesList] = new SelectList(am.FindUpcomingStates()。ToList(), state_id, state_Name);
return View();
}
我的型号是
public IQueryable< state> FindUpcomingStates()
{
// 从状态返回Adm.states orderby state.state_name select州;
}
点击提交按钮后,项目state_id将保存到数据库中。
我在Controller中写的如下,但是我得到了真值或假值,我想要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);
}
谢谢,
解决方案
hi
i在vb.net windows应用程序中完成此操作,下面的代码只是编辑为你尝试运行
-------- -------- INSERT ----------------------------
Dim a1,a2, a3 as boolean
Dim a4 as string
a1 = CheckBox1.CheckState
a2 = CheckBox2.CheckState
a3 = CheckBox3.CheckState
如果a1 = True那么
a1 =真
否则
a1 =假
结束如果
如果a2 = True那么
a2 =真
否则
a2 = False
结束如果
如果a3 = True那么
a3 =真
否则
a3 = False
结束如果
a4 = a1&| &a2&| &a3
'现在你可以轻松地将a4值作为字符串存储到数据库中
'它在db中存储值如0 | 1 | 0
'表示真状态为0假状态1
---------------- -REtreive -----------------
'现在您检索数据库值作为数据表或数据集我使用'datadable object dt这里是下面的代码
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。拆分(|)
对于j = 0到a.Length - 1
arrs = a(j)
如果j = com并且arrs =True那么
CheckBox1.CheckState = True
ElseIf j = com + 1并且arrs =True然后
CheckBox2.CheckState = True
ElseIf j = com + 2并且arrs =True然后
CheckBox3.CheckState = True
结束如果
如果j = com并且arrs =False那么
CheckBox1.CheckState = False
ElseIf j = com + 1并且arrs =False然后
CheckBox2.CheckState = False
ElseIf j = com + 2并且arrs =False然后
CheckBox3.CheckState = False
结束如果
下一步
[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, < span class =code-string> state_Name,_ stateName);
}
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,
解决方案
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
[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); }
这篇关于如何在数据库中保存复选框选中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文