简体   繁体   中英

Passing a model class through a checkbox value to the controller

My test class has a list of test2 classes as a one of its members and when a checkbox is checked, I would like to pass each of the test2 classes back to the controllers.

EDIT: I updated the model from report chooser below to test to match my description above.

model test

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;

        }
    }
}
}

model test2

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

namespace ImpactIndicator.Models
{
public class test2
{
    public string SchoolName { get; set; }
    public string DistName { get; set; }
    public int SchoolID { get; set; }
    public int DistID { get; set; }
    public int? SchoolLevelID { get; set; }
}
}

view

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++;
}

you have the same name on your checkboxes so you can do

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

on your controller and this will give you a list of all checked checkboxes from that list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM