Ich habe angefangen, Code in Razor Pages zu schreiben, und ich möchte eine CheckBox-Liste erstellen, aber das funktioniert nicht. Mein Code von der (Seite) .cshtml.cs Datei, die die Liste setzt:Wie kann ich diese CheckBox-Liste in asp.net arbeiten lassen?
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 }
};
Mein Code von der (Seite) .cshtml-Datei, die diese checkbox'es vorzuweisen hat:
<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>
Und wieder von meiner (Seite) .cshtml.cs wo ich versuche, die gewählt checkbox'es auf meinem Objekt zu setzen:
Uczestnik.Kontakt = Kontakty.Where(x => x.IsChecked).Select(x => x.Name).ToList();
ich habe keine Ahnung, warum dies nicht funktioniert. Danke für die Hilfe
EDIT: Ich habe vergessen zu sagen, was los ist. Wenn ich einen brak Punkt gesetzt auf
Uczestnik.Kontakt = Kontakty.Where(x => x.IsChecked).Select(x => x.Name).ToList();
zeigt es, dass kein Element in Kontakty hat es Wert als wahr isChecked ist, auch wenn ich es überprüfen und klicken Sie senden, dass Code in einer Methode OnPost ist
EDIT2: hier zeige ich mehr des Codes
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);
}
}
Dieses von meiner AnkietaGlowna.cshtml ist:
<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>
Und mein ModelForCheckBoxList
public class ModelForCheckBoxList
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsChecked { get; set; }
}
Wie ich schon sagte, bevor das Problem ist, wird die Kontakty.IsChecked nicht ändern, wenn ich es in Form überprüfen und klicken
"complete" einreichen, minimales Beispiel plz. – Steve