如何在MVC3(Aspx)Asp.Net(C#)中添加复选框? [英] How to add Checkbox in MVC3(Aspx) Asp.Net(C#)?

查看:72
本文介绍了如何在MVC3(Aspx)Asp.Net(C#)中添加复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想在应用程序中添加复选框.. MVC-3(Aspx)Asp.Net(C#)
我要求添加复选框并保存所选复选框的值..

但是我什至不知道如何向数据库添加复选框.

有人可以帮我吗?

在此先感谢..

Hi all,

I wants to add Checkboxes in my application..MVC-3(Aspx) Asp.Net(C#)
I asked to add checkboxes and and save the values of that checkboxes which are selected..

But i even don''t know how to add checkbox to the database.

can anyone help me for that?

Thanks in advance..

推荐答案

有两种方法可以实现您的功能.

1.您可以使用字符串属性构建模型,并使用所需的html内容添加<input type="checkbox"></input>控件,然后从视图中的模型进行呈现.
There are two ways to achieve your functionality.

1. You can build your model with string property and add <input type="checkbox"></input> controls with html stuff you need and then render it from model in view.
public class checkboxes
{
  public string checks{get; set;}
}


然后,您需要从控制器传递此模型以查看和访问它,如下所示.


Then you need pass this model from controller to view and access it like following.

<%: Model.checkboxes %>


这样,它将根据需要呈现尽可能多的复选框.

2.第二种方法与第一种有关,但有所不同.在这种方法中,您只需要传递带有复选框某些参数而不是html代码的模型.然后根据这些参数,在视图上构建复选框控件.


In that way it will render as many as checkboxes as you want.

2. Second method is related to first but with some difference. In this method you need to pass model with only some parameters of checkboxes not the html code. And from those parameters we are building checkbox control on the view.

public class checkboxes
{
  public List<string> checksname{get; set;}
}</string>


然后在视图中,您只需遍历名称并创建复选框.


Then in view you just loop through names and create checkboxes.

<% foreach(string checkname in Model.checkboxes)
{ %>
<%: Html.CheckBox(checkname, false) %>
<% } %>


在视图中添加:
in the view add:
<input style="margin: 0px;padding: 0px;width: 15px;height: 15px; border: 0px; background: none;" type="checkbox" name="RememberMe" id="RememberMe" value="true" />


在控制器中,您可以看到以下复选框:


and in the controller you can get the vale of checkbox as:

public ActionResult LogOn(bool? rememberMe)
        {           
                bool remember = Convert.ToBoolean(rememberMe);
...
}


现在可以将bool类型变量保存在数据库中,为此,创建一列类型为bit的列


and now the bool type variable you can save in database , for this create a column of type bit


将列(如选中的)添加到数据库表中.
以该字段的数据类型为位".
如果选中复选框,则通过"True"
如果未选中,则传递"False"
Add column (like checked) to your database table.
Take data type of that field as "Bit".
if checkbox is cheked then pass "True"
if unchecked pass "False"


这篇关于如何在MVC3(Aspx)Asp.Net(C#)中添加复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆