簡體   English   中英

如何使asp.net中的此CheckBox列表起作用?

[英]How can I make this CheckBox list in asp.net work?

我開始在Razor Pages中編寫一些代碼,並且想創建一個CheckBox列表,但這不起作用。 我從(page).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 }
    };

我來自(page).cshtml文件的代碼,其中必須顯示這些復選框:

<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>

再次從我的(page).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並未更改。

@{       
        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>
        }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM