如何获取复选框值回VIEW [英] How to get CheckBox values back to VIEW

查看:136
本文介绍了如何获取复选框值回VIEW的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的codeS及其说明,24条记录在数据库

的完整列表

我从DB一个选择使用存储过程,并将其输出到视图()。

在用户点击这些复选框,他多少希望,它被张贴到数据库到另一个用户表中。

我通过这个用户表扫描得到,他已经检查了数据..说出来的24他检查10复选框。

我希望能够输出所有与用户进行了检查,相关的盒子的复选框。我有数据,其中所有的复选框处于坐在VIEWBAG列表。我也有用户表,其中包含他还检查了seprate列表,在一个VIEWBAG,这样我可以访问此在视图中的数据。

我如何去输出所有复选框也被检查通过点击使用来自控制器通过VIEWBAG用户的复选框?

下面code,只是遍历所有的数据,并输出所有复选框。我需要确保在新的VIEWBAG.USER的那些用于检查下面的相关框:

  @foreach(在ViewBag.Expertise VAR项)
    {
        L_COUNT = L_COUNT + 1;
        L_ code = L_ code + 1;        如果(L_COUNT == 1)
        {
            < TD类=tdcheckbox>
                <输入类型=复选框NAME =code @(L_ code)VALUE =@ item.expertise code/>
                < B> @ item.expertiseDesc< / B>
            < / TD>
            否则,如果(L_COUNT 3;)
            {
                < TD类=tdcheckbox>
                    <输入类型=复选框NAME =code @(L_ code)VALUE =@ item.expertise code/>
                    < B> @ item.expertiseDesc< / B>
                < / TD>
            }
            否则,如果(L_COUNT == 3)
            {
                < TD类=tdcheckbox>
                    <输入类型=复选框NAME =code @(L_ code)VALUE =@ item.expertise code/>
                    < B> @ item.expertiseDesc< / B>
                < / TD>
            }
            否则,如果(L_COUNT%2 == 0安培;&安培; L_COUNT == 4)
            {
                < TD类=tdcheckbox>
                    <输入类型=复选框NAME =code @(L_ code)VALUE =@ item.expertise code/>
                    < B> @ item.expertiseDesc< / B>                @if(@ item.expertise code ==OTHER)
                {
                    @ Html.TextBox(@ item.otherExpertise)
                    < BR /><字体颜色=红色>逗号分隔专门知识和LT; / FONT>
                }
                < / TD>                @:或其可TR>&下; / TR>
                L_COUNT = 0;


解决方案

如果我理解你的问题正确,你可以做以下。

把这个在您的视图的顶部

  @ {VAR IDS =(列表< INT>)ViewBag.USERS);

然后,当你通过所有复选框循环就可以做到以下几点。

 如果(IDS = NULL&放大器;!&安培; ids.Contains(item.expertise code)条)
{
   <输入类型=复选框NAME =code @(L_ code)VALUE =@ item.expertise code检查/>
}
其他
{
  <输入类型=复选框NAME =code @(L_ code)VALUE =@ item.expertise code/>
}

I have a full list of codes and their descriptions, 24 records in a DB

I do a select from the DB using stored Proc, and output this to the VIEW().

The user clicks on these checkboxes, how many he wishes, and it gets posted to the DB into another User table.

I scan through this User Table to get the data that he has checked .. say out of 24 he checks 10 checkboxes.

I want to be able to output all the checkboxes with the relevant boxes that were checked by the user. I have the data, where ALL the checkboxes are in a list sitting in VIEWBAG. and i also have the User Table that contains the data that he checked also in a seprate List, in a VIEWBAG so that i can access this in the VIEW.

How do i go about outputting ALL the checkboxes but also checking the checkboxes that were clicked by the user using the VIEWBAG passed from the controller?

The below code, just loops through ALL the data and outputs ALL checkboxes. I need to make sure the ones in the new VIEWBAG.USER are used to check the relevant boxes below:

    @foreach (var item in ViewBag.Expertise)
    {
        l_count = l_count + 1;
        l_code = l_code + 1;

        if (l_count == 1)
        {                                                          
            <td class="tdcheckbox">
                <input type="checkbox" name="code@(l_code)" value="@item.expertiseCode" />
                <b>@item.expertiseDesc</b>
            </td> 
            else if (l_count < 3)
            {    
                <td class="tdcheckbox">
                    <input type="checkbox" name="code@(l_code)" value="@item.expertiseCode" />
                    <b>@item.expertiseDesc</b>
                </td>
            }
            else if (l_count == 3)
            {           
                <td class="tdcheckbox">
                    <input type="checkbox" name="code@(l_code)" value="@item.expertiseCode" />
                    <b>@item.expertiseDesc</b>
                </td>                                                            
            }
            else if (l_count % 2 == 0 && l_count == 4)
            {         
                <td class="tdcheckbox">
                    <input type="checkbox" name="code@(l_code)" value="@item.expertiseCode" />
                    <b>@item.expertiseDesc</b>

                @if (@item.expertiseCode == "OTHER")
                {
                    @Html.TextBox("@item.otherExpertise")
                    <br /><font color="red">Comma separate specific expertise</font>
                }
                </td>   

                @:<tr></tr>
                l_count = 0;

解决方案

If I understand you question properly you can do the following.

put this at the top of your view

@{var ids = (List<int>)ViewBag.USERS);

Then when your are looping through all the checkboxes you can do the following.

if (ids != null && ids.Contains(item.expertiseCode))
{
   <input type="checkbox" name="code@(l_code)" value="@item.expertiseCode" checked />
}
else
{
  <input type="checkbox" name="code@(l_code)" value="@item.expertiseCode" />
}

这篇关于如何获取复选框值回VIEW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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