如何在ASP.NET Core中实现复选框列表?

编程入门 行业动态 更新时间:2024-10-27 10:33:54
本文介绍了如何在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中实现复选框列表?

本文发布于:2023-10-12 01:37:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483395.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   如何在   列表   NET   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!