<legend id='sRqnN'><style id='sRqnN'><dir id='sRqnN'><q id='sRqnN'></q></dir></style></legend>
    1. <i id='sRqnN'><tr id='sRqnN'><dt id='sRqnN'><q id='sRqnN'><span id='sRqnN'><b id='sRqnN'><form id='sRqnN'><ins id='sRqnN'></ins><ul id='sRqnN'></ul><sub id='sRqnN'></sub></form><legend id='sRqnN'></legend><bdo id='sRqnN'><pre id='sRqnN'><center id='sRqnN'></center></pre></bdo></b><th id='sRqnN'></th></span></q></dt></tr></i><div id='sRqnN'><tfoot id='sRqnN'></tfoot><dl id='sRqnN'><fieldset id='sRqnN'></fieldset></dl></div>

      <small id='sRqnN'></small><noframes id='sRqnN'>

    2. <tfoot id='sRqnN'></tfoot>
        <bdo id='sRqnN'></bdo><ul id='sRqnN'></ul>
    3. ASP.NET 5 MVC 6 中的单选按钮标记帮助程序

      Radio Button Tag Helpers in ASP.NET 5 MVC 6(ASP.NET 5 MVC 6 中的单选按钮标记帮助程序)

          <tbody id='N9BGf'></tbody>

      1. <i id='N9BGf'><tr id='N9BGf'><dt id='N9BGf'><q id='N9BGf'><span id='N9BGf'><b id='N9BGf'><form id='N9BGf'><ins id='N9BGf'></ins><ul id='N9BGf'></ul><sub id='N9BGf'></sub></form><legend id='N9BGf'></legend><bdo id='N9BGf'><pre id='N9BGf'><center id='N9BGf'></center></pre></bdo></b><th id='N9BGf'></th></span></q></dt></tr></i><div id='N9BGf'><tfoot id='N9BGf'></tfoot><dl id='N9BGf'><fieldset id='N9BGf'></fieldset></dl></div>

          • <tfoot id='N9BGf'></tfoot>
          • <legend id='N9BGf'><style id='N9BGf'><dir id='N9BGf'><q id='N9BGf'></q></dir></style></legend>
              <bdo id='N9BGf'></bdo><ul id='N9BGf'></ul>

            • <small id='N9BGf'></small><noframes id='N9BGf'>

                本文介绍了ASP.NET 5 MVC 6 中的单选按钮标记帮助程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在 ASP.NET 5 MVC 6 中看不到任何单选按钮的标签助手.在我需要使用单选按钮的情况下,处理表单元素的正确方法是什么?

                I don't see any tag helpers for radio buttons in ASP.NET 5 MVC 6. What's the right way of handling form elements where I need to use radio buttons?

                推荐答案

                所有输入类型都有一个 TagHelper,包括单选按钮类型.假设您有这样的视图模型

                There is a TagHelper for all the input types which includes the radio button type as well. Assuming you have a view model like this

                public class CreateUserVm
                {
                    public string UserName { set; get; }
                    public IEnumerable<RoleVm> Roles { set; get; } 
                    public int SelectedRole { set; get; }
                }
                public class RoleVm
                {
                    public int Id { set; get; }
                    public string RoleName { set; get; }        
                }
                

                在您的 GET 操作中,

                And in your GET action,

                public IActionResult Index()
                {
                    var vm = new CreateUserVm
                    {
                        Roles = new List<RoleVm>
                        {
                            new RoleVm {Id = 1, RoleName = "Admin"},
                            new RoleVm {Id = 2, RoleName = "Editor"},
                            new RoleVm {Id = 3, RoleName = "Reader"}
                        }
                    };
                    return View(vm);
                }
                

                在视图中,您可以简单地对输入标签使用标记.

                In the view, You can simply use markup for the input tag.

                @model YourNamespaceHere.CreateUserVm
                <form asp-action="Index" asp-controller="Home">
                    <label class="label">User name</label>
                    <div class="col-md-10">
                        <input type="text" asp-for="UserName" />
                    </div>
                    <label class="label">Select a Role</label>
                    <div class="col-md-10">
                    @foreach (var item in Model.Roles)
                    {
                       <input asp-for="SelectedRole" type="radio" value="@item.Id" /> @item.RoleName
                    }
                    </div>
                    <input type="submit" />
                </form>
                

                当您发布表单时,所选角色的 Rold Id 将位于 SelectedRole 属性中

                When you post your form, The Rold Id for the selected role will be in the SelectedRole property

                请记住,上面的 razor 代码将为每个生成的输入生成具有 same Id 属性值和 name 属性值的输入元素由循环.在上面的示例中,它将生成 3 个输入元素(单选按钮类型),其中 Idname 属性值设置为 SelectedRole.当 name 属性值与视图模型中的属性名称(SelectedRole)匹配时,模型绑定将起作用,但重复的 Id 属性值可能会给您的客户端代码带来麻烦(文档中的重复 ID 无效)

                Keep in mind that, the above razor code will generate input element with same Id attribute value and name attribute value for each input generated by the loop. In the above example, it will generate 3 input elements(radio button type) with the Id and name attribute value set to SelectedRole. Model binding will work as the name attribute value matches with the property name(SelectedRole) in our view model, but the duplicate Id attribute values might give you trouble with client side code (duplicate Ids in a document is invalid)

                这篇关于ASP.NET 5 MVC 6 中的单选按钮标记帮助程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                Accessing attribute info from DTE(从 DTE 访问属性信息)
                c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                C# Attributes and their uses(C# 属性及其用途)
                C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)

                  <tbody id='c4eZP'></tbody>
                <legend id='c4eZP'><style id='c4eZP'><dir id='c4eZP'><q id='c4eZP'></q></dir></style></legend>

                <i id='c4eZP'><tr id='c4eZP'><dt id='c4eZP'><q id='c4eZP'><span id='c4eZP'><b id='c4eZP'><form id='c4eZP'><ins id='c4eZP'></ins><ul id='c4eZP'></ul><sub id='c4eZP'></sub></form><legend id='c4eZP'></legend><bdo id='c4eZP'><pre id='c4eZP'><center id='c4eZP'></center></pre></bdo></b><th id='c4eZP'></th></span></q></dt></tr></i><div id='c4eZP'><tfoot id='c4eZP'></tfoot><dl id='c4eZP'><fieldset id='c4eZP'></fieldset></dl></div>
                • <small id='c4eZP'></small><noframes id='c4eZP'>

                    • <bdo id='c4eZP'></bdo><ul id='c4eZP'></ul>
                    • <tfoot id='c4eZP'></tfoot>