本文介绍了如何在ASP.NET Core中实现复选框列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望在ASP.NET Core中实现一个复选框列表,但遇到了一些困难.
I am looking to implement a checkboxlist in ASP.NET Core, but am facing some difficulties.
我的ViewModel:
My ViewModel:
public class GroupIndexViewModel { public Filter[] Filters { get; set; } } public class Filter { public int Id { get; set; } public string Name { get; set; } public bool Selected { get; set; } }我的观点:
@model GroupIndexViewModel <form asp-action="Index" asp-controller="Group" method="get"> <ul> @for (var i = 0; i < Model.Filters.Length; i++) { <li> <input type="checkbox" id="@Model.Filters[i].Name" asp-for="@Model.Filters[i].Selected" value="@Model.Filters[i].Selected" checked="@Model.Filters[i].Selected" /> <label for="@Model.Filters[i].Name">@Model.Filters[i].Name</label> </li> } </ul> <button type="submit" name="action">Filtrer</button> </form>发布到我的控制器时,即使在视图中被选中,我的视图模型中的Filter属性也会显示为false.
When posting to my controller, the Filter property in my viewmodel shows selected false even though it is selected in the view.
推荐答案我将按照以下方式进行操作.
I would do following way.
@model GroupIndexViewModel <form asp-action="Index" asp-controller="Group" method="get"> <ul> @for (var i = 0; i < Model.Filters.Count; i++) { <li> <input type="checkbox" asp-for="@Model.Filters[i].Selected" /> <label asp-for="@Model.Filters[i].Selected">@Model.Filters[i].Name</label> <input type="hidden" asp-for="@Model.Filters[i].Id" /> <input type="hidden" asp-for="@Model.Filters[i].Name" /> </li> } </ul> <button type="submit" name="action">Filtrer</button> </form>在这里,我假设您已正确执行控制器和操作.
Here I assuming that you have proper implementation of controller and action.
更多推荐
如何在ASP.NET Core中实现复选框列表?
发布评论