如何在asp.net中使这个CheckBox列表工作?

时间:2017-11-24 21:42:26

标签: c# .net razor

我开始在Razor Pages中编写一些代码,我想制作一个CheckBox列表,但这不起作用。 我的代码来自设置列表的(页面).cshtml.cs文件:

public List<ModelForCheckBoxList> Kontakty = new List<ModelForCheckBoxList>
    {
        new ModelForCheckBoxList{Id = 1, Name = "E-Mail", IsChecked = false },
        new ModelForCheckBoxList{Id = 2, Name = "Telefon", IsChecked = false },
        new ModelForCheckBoxList{Id = 3, Name = "Osobisty", IsChecked = false }
    };

我的代码来自(页面).cshtml文件,必须显示这些复选框&#39; es:

<div class="form-group">
            @Html.LabelFor(x => x.Uczestnik.Kontakt)
            @for (int index = 0; index < Model.Kontakty.Count; index++)
                {
                <div class="form-control">
                    @Html.HiddenFor(x => x.Kontakty[index].Id)
                    @Html.CheckBoxFor(x => x.Kontakty[index].IsChecked)
                    @Html.DisplayFor(x => x.Kontakty[index].Name)
                </div>
                }
        </div>

再次从我的(页面).cshtml.cs中,我尝试在我的对象上设置所选的复选框:

Uczestnik.Kontakt = Kontakty.Where(x => x.IsChecked).Select(x => x.Name).ToList();

我不知道为什么这不起作用。 谢谢你的帮助

编辑:我忘记说错了什么。当我在

上设置制动点时
Uczestnik.Kontakt = Kontakty.Where(x => x.IsChecked).Select(x => x.Name).ToList();

它表明Kontakty中没有任何项目的isChecked值为true,即使我检查它并单击send,该代码也在OnPost方法中

EDIT2:这里我展示了更多代码

public class AnkietaGlownaModel : PageModel
{
    public Uczestnik Uczestnik { get; set; }


    public List<ModelForCheckBoxList> Kontakty = new List<ModelForCheckBoxList>
    {
        new ModelForCheckBoxList{Id = 1, Name = "E-Mail", IsChecked = false },
        new ModelForCheckBoxList{Id = 2, Name = "Telefon", IsChecked = false },
        new ModelForCheckBoxList{Id = 3, Name = "Osobisty", IsChecked = false }
    };

    public void OnGet()
    {

    }

    public ActionResult OnPost()
    {
        if (!ModelState.IsValid)
            return Page();

        Uczestnik.Kontakt = Kontakty.Where(x => x.IsChecked).Select(x => x.Name).ToList();

        return RedirectToPage("/Index", Uczestnik);
    }
}

这是来自我的AnkietaGlowna.cshtml:

<form method="post">
<div class="panel panel-default">
    <div class="panel-heading text-center"><h4>Ankieta dla klientów</h4></div>
    <div class="panel-body" style="background-color: lightgrey">
        <div class="form-group">
            @Html.LabelFor(x => x.Uczestnik.Imie)
            @Html.TextBoxFor(x => x.Uczestnik.Imie, new { @class = "form-control" })
            @Html.ValidationMessageFor(x => x.Uczestnik.Imie, null, new { @class = "text-danger"})
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Uczestnik.Plec)
            @Html.DropDownListFor(x => x.Uczestnik.Plec, new[] {

               new SelectListItem()
               {
                   Text = "Mężczyzna",
                   Value = "Mężczyzna"
               },
               new SelectListItem()
               {

                   Text = "Kobieta",
                   Value = "Kobieta"
               },
               new SelectListItem()
               {
                   Text = "Nie podaję",
                   Value = "Nie Podano",
                   Selected = true
               } }, new { @class = "form-control" })
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Uczestnik.Email)
            @Html.TextBoxFor(x => x.Uczestnik.Email, new { @class = "form-control" })
            @Html.ValidationMessageFor(x => x.Uczestnik.Email, null, new { @class = "text-danger" })
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Uczestnik.NumerTelefonu)
            @Html.TextBoxFor(x => x.Uczestnik.NumerTelefonu, new { @class = "form-control" })
            @Html.ValidationMessageFor(x => x.Uczestnik.NumerTelefonu, null, new { @class = "text-danger" })
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Uczestnik.Ulica)
            @Html.TextBoxFor(x => x.Uczestnik.Ulica, new { @class = "form-control" })
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Uczestnik.NumerDomu)
            @Html.TextBoxFor(x => x.Uczestnik.NumerDomu, new { @class = "form-control" })
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Uczestnik.NumerMieszkania)
            @Html.TextBoxFor(x => x.Uczestnik.NumerMieszkania, new { @class = "form-control" })
            @Html.ValidationMessageFor(x => x.Uczestnik.NumerMieszkania, null, new { @class = "text-danger" })
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Uczestnik.KodPocztowy)
            @Html.TextBoxFor(x => x.Uczestnik.KodPocztowy, new { @class = "form-control" })
            @Html.ValidationMessageFor(x => x.Uczestnik.KodPocztowy, null, new { @class = "text-danger" })
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Uczestnik.Miejscowosc)
            @Html.TextBoxFor(x => x.Uczestnik.Miejscowosc, new { @class = "form-control" })
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Uczestnik.Kontakt)
            @for (int index = 0; index < Model.Kontakty.Count; index++)
                {
                <div class="form-control">
                    @Html.HiddenFor(x => x.Kontakty[index].Id)
                    @Html.CheckBoxFor(x => x.Kontakty[index].IsChecked)
                    @Html.DisplayFor(x => x.Kontakty[index].Name)
                </div>
                }
        </div>

        <div class="panel-footer">
            <button type="submit" class="btn btn-success center-block">Wyślij ankietę</button>
        </div>
    </div>
</div>

我的ModelForCheckBoxList

public class ModelForCheckBoxList
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsChecked { get; set; }
}

正如我之前所说,问题是,当我在表单中检查并点击提交时,Kontakty.IsChecked没有改变

1 个答案:

答案 0 :(得分:0)

@{       
        string strChecked = "checked";

        foreach (var item in Model.Kontakty)
        {
            <input type="checkbox" name="SUMBIT NAME" value="@item.Id" @if (item.IsChecked) { @strChecked } />
            <a>@item.Name</a>
        }

}