前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TagHelper+Layui封装组件之Radio单选框

TagHelper+Layui封装组件之Radio单选框

作者头像
易墨
发布2018-09-14 15:54:06
1.4K0
发布2018-09-14 15:54:06
举报

TagHelper+Layui封装组件之Radio单选框

  • 标签名称:cl-radio
  • 标签属性:
    • asp-for:绑定的字段,必须指定
    • asp-items:绑定单选项 类型为:IEnumerable<SelectListItem> 太简单了,直接上代码了

RadioTagHelper代码

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace LayuiTagHelper.TagHelpers
{
    /// <summary>
    /// 单选框
    /// </summary>
    [HtmlTargetElement(RadioTagName)]
    public class RadioTagHelper : TagHelper
    {
        private const string RadioTagName = "cl-radio";
        private const string ForAttributeName = "asp-for";
        private const string ItemsAttributeName = "asp-items";

        [ViewContext]
        public ViewContext ViewContext { get; set; }

        [HtmlAttributeName(ForAttributeName)]
        public ModelExpression For { get; set; }

        [HtmlAttributeName(ItemsAttributeName)]
        public IEnumerable<SelectListItem> Items { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (For == null)
            {
                throw new ArgumentException("必须绑定模型");
            }
            foreach (var item in Items)
            {
                var radio = new TagBuilder("input");
                radio.TagRenderMode = TagRenderMode.SelfClosing;
                radio.Attributes.Add("id", ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(For.Name));
                radio.Attributes.Add("name", ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(For.Name));
                radio.Attributes.Add("value", item.Value);
                radio.Attributes.Add("title", item.Text);
                radio.Attributes.Add("type", "radio");
                if (item.Disabled)
                {
                    radio.Attributes.Add("disabled", "disabled");
                }
                if (item.Selected || item.Value == For.Model?.ToString())
                {
                    radio.Attributes.Add("checked", "checked");
                }
                output.Content.AppendHtml(radio);
            }
            output.TagName = "";
        }
    }
}

使用示例

@{
string sex="男";
var Items=new List<SelectListItem>()
           {
                new SelectListItem() { Text = "男", Value = "男" },
                new SelectListItem() { Text = "女", Value = "女"},
                new SelectListItem() { Text = "不详", Value = "不详",Disabled=true }
           };
}
<cl-radio asp-items="@Items" asp-for="sex"></cl-radio>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-10-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • TagHelper+Layui封装组件之Radio单选框
  • RadioTagHelper代码
  • 使用示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档