Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过复选框值将模型类传递给控制器_C#_Asp.net Mvc 4 - Fatal编程技术网

C# 通过复选框值将模型类传递给控制器

C# 通过复选框值将模型类传递给控制器,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我的测试类有一个test2类列表作为其成员之一,当选中复选框时,我希望将每个test2类传递回控制器 编辑:我从下面的报告选择器更新了模型,以进行测试,以匹配上面的描述 模型试验 using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; namespace ImpactIndicator.Models { publ

我的测试类有一个test2类列表作为其成员之一,当选中复选框时,我希望将每个test2类传递回控制器

编辑:我从下面的报告选择器更新了模型,以进行测试,以匹配上面的描述

模型试验

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace ImpactIndicator.Models
{
public enum ComparisonSideBySideValue { School, Entry };

public class test
{
    public List<test2> Schools
    {
        get
        {
            if (HttpContext.Current.Session["ReportChooser_SchoolInfo"] != null)
            {
                List<ReportSchoolInfo> schools=(List<ReportSchoolInfo>)HttpContext.Current.Session["ReportChooser_SchoolInfo"];
                schools.OrderBy(t=>t.DistName).ThenBy(t=>t.SchoolName);
                return schools;
            }
            else
                return null;
        }
        set
        {
            HttpContext.Current.Session["ReportChooser_SchoolInfo"] = value;

        }
    }
}
}
看法

int计数器1=0;
foreach(ViewBag.ReportSchools中的var项)
{
如果(计数器1%2==0)
{
@:
}
@{
bool-isSelected=false;
如果(ViewBag.ReportChooser.Schools!=null)
{
foreach(ViewBag.ReportChooser.Schools中的变量selSchoolID)
{
if(item.ToString()==selSchoolID)
{
isSelected=true;
打破
}
}
}
如果(当选)
{
@:@item.DistName//@item.SchoolName
}
其他的
{
@:@item.DistName//@item.SchoolName
}
}
@*@Html.CheckBox(schoolName,new{@class=“rptschool”})@item.DistName//item.schoolName*@
if(计数器1%2==1 | |计数器1==ViewBag.SchoolCount-1)
{
@:
}
计数器1++;
}

您的复选框上有相同的名称,因此您可以

Request.Form["Schools"].ToString() 

在控制器上,这将为您提供该列表中所有选中复选框的列表

好的,谢谢。有没有办法将这些值保存到测试类的列表中?像下面这样的?测试t=新测试();foreach(Request.Form[“Schools”]){t.test2.add(te);}是的,Request.Form将带来1,2,。。。因此,您需要拆分它test t=new test();foreach(test2te在Request.Form[“Schools”].ToString().split(','){t.test2.add(te);}它告诉我它不能将foreach代码行的类型“string”转换为“ImpactIndicator.Models.test2”。split将给出一个字符串列表。所以在请求中将foreach更改为字符串temp。。。然后,对于add,您需要执行t.test2.add(newtest2{field=temp})好的,保存到请求中的内容。表单[“Schools”]是test2类的一组选择。如果我是正确的,在上面的示例中,该字段是test2类中的一个成员,temp是Request.Form[“Schools”]中的每个拆分字符串,因此我会将test2类的一个成员设置为test2类选择之一的字符串表示形式
int counter1 = 0;

foreach (var item in ViewBag.ReportSchools)
{
    if (counter1 % 2 == 0)
    {
                    @:<tr>
                }

                <td>

                @{
    bool isSelected = false;
    if (ViewBag.ReportChooser.Schools != null)
    {
        foreach (var selSchoolID in ViewBag.ReportChooser.Schools)
        {
            if (item.ToString() == selSchoolID)
            {
                isSelected = true;
                break;
            }
        }

    }
    if (isSelected)
    {
                        <input type='checkbox' name='Schools' id='Schools' value="@item" class="rptschool" checked="checked" /> @:@item.DistName/@item.SchoolName &nbsp;&nbsp;
                    }
    else
    {
                        <input type='checkbox' name='Schools' id='Schools' value="@item" class="rptschool" /> @:@item.DistName/@item.SchoolName &nbsp;&nbsp;
                    }

                    }

                @*@Html.CheckBox(schoolName, new { @class = "rptschool" }) @item.DistName/@item.SchoolName &nbsp;&nbsp; *@
                </td>

    if (counter1 % 2 == 1 || counter1 == ViewBag.SchoolCount - 1)
    {
                    @:</tr>
                }

    counter1++;
}
Request.Form["Schools"].ToString()