asp.net MVC2怎么得的选中复选框的值保存到数据库?
2个回答
展开全部
view:
@using (Html.BeginForm())
{
<table>
<tr>
<th>
<input type="checkbox" id="checkedAll" value="" />全选
<input type="hidden" id="valuelist" name="valuelist" />
</th>
</tr>
<tbody align="center">
<tr>
<td>
<input name="cBox" type="checkbox" value="1" />
<input name="cBox" type="checkbox" value="2" />
<input name="cBox" type="checkbox" value="3" />
<input name="cBox" type="checkbox" value="4" />
</td>
</tr>
</tbody>
</table>
}
controller:
public ActionResult Index(long[] cBox)
{
for (int i = 0; i < cBox.Length; i++) //查看是否有外键关联
{
if (Factory.Apply_BLL.CheckFKByApplyId(cBox[i]))
{
AddValidatorError("有数据存在外键与其关联,不能删除");
return RedirectToAction("ApplyIndex");
}
}
for (int i = 0; i < cBox.Length; i++)
{
var apply = Factory.Apply_BLL.GetApplyById(cBox[i]);
Factory.Apply_BLL.ApplyDelete(apply);
}
Factory.Apply_BLL.Save();
if (cBox.Length > 1) AddSuccessMsg("批量删除成功");
else AddSuccessMsg("删除成功");
return RedirectToAction("ApplyIndex");
}
@using (Html.BeginForm())
{
<table>
<tr>
<th>
<input type="checkbox" id="checkedAll" value="" />全选
<input type="hidden" id="valuelist" name="valuelist" />
</th>
</tr>
<tbody align="center">
<tr>
<td>
<input name="cBox" type="checkbox" value="1" />
<input name="cBox" type="checkbox" value="2" />
<input name="cBox" type="checkbox" value="3" />
<input name="cBox" type="checkbox" value="4" />
</td>
</tr>
</tbody>
</table>
}
controller:
public ActionResult Index(long[] cBox)
{
for (int i = 0; i < cBox.Length; i++) //查看是否有外键关联
{
if (Factory.Apply_BLL.CheckFKByApplyId(cBox[i]))
{
AddValidatorError("有数据存在外键与其关联,不能删除");
return RedirectToAction("ApplyIndex");
}
}
for (int i = 0; i < cBox.Length; i++)
{
var apply = Factory.Apply_BLL.GetApplyById(cBox[i]);
Factory.Apply_BLL.ApplyDelete(apply);
}
Factory.Apply_BLL.Save();
if (cBox.Length > 1) AddSuccessMsg("批量删除成功");
else AddSuccessMsg("删除成功");
return RedirectToAction("ApplyIndex");
}
追问
谢谢啊。保存已经做好了,现在有个问题是我要页面加载时把数据库已经保存好的复选框勾上。怎么做啊?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询