【问题标题】:Razor Pages ASP.NET Core 3.1 - How To Dynamically create multiple radio button groupsRazor Pages ASP.NET Core 3.1 - 如何动态创建多个单选按钮组
【发布时间】:2021-02-27 18:16:31
【问题描述】:

我需要在一个页面中动态生成多个单选按钮组并检索它们的值。谁能提供一个简单的例子?网上没找到。

【问题讨论】:

标签: asp.net-core razor-pages asp.net-core-3.1


【解决方案1】:

这是一个关于在 razor 页面中使用单选按钮和发布选定单选按钮值的演示:

cshtml.cs:

[BindProperty]
        public string Selected { get; set; }
        public List<string> Radios { get; set; }
        public IActionResult OnGet()
        {
            //You can dynamically set Radios value here
            Radios = new List<string> { "0-10", "11-20", "21-30" };
            return Page();
        }
        public IActionResult OnPost()
        {
            return OnGet();
        }

cshtml:

<form method="post">
        Age:<br />
        @foreach (var radio in Model.Radios)
        {
            <input type="radio" asp-for="Selected" value="@radio" />@radio<br />
        }
        <input type="submit" value="submit" />
    </form>

结果:

【讨论】:

    猜你喜欢
    • 2018-10-16
    • 2019-04-05
    • 1970-01-01
    • 2021-04-28
    • 2020-11-23
    • 2011-07-28
    • 2020-11-08
    • 1970-01-01
    • 2018-05-09
    相关资源
    最近更新 更多