zoukankan      html  css  js  c++  java
  • three ways creating custom helpers to show RadioButtonList in MVC

    first way is using the extension method to build the helper:    

     public class ListOption
        {
            public string Text { get; set; }
            public object Value { get; set; }
        }
    
        public static class CustomHelperExtensions
        {
            public static MvcHtmlString RadioButtonList(this HtmlHelper helper, string name, object selectedValue, params ListOption[] options)
            {
                int i = 0;
    
                StringBuilder builder = new StringBuilder();
    
                foreach (var option in options)
                {
                    bool isChecked = selectedValue == null ? false : selectedValue.ToString() == option.Value.ToString();
                    var controlId = name + i;
                    var mvcString = helper.RadioButton(name, option.Value, isChecked, new { id = controlId });
    
                    builder.AppendLine(mvcString.ToHtmlString());
    
                    TagBuilder tag = new TagBuilder("label");
                    tag.MergeAttribute("for", controlId);
                    tag.InnerHtml = option.Text;
    
                    builder.AppendLine(tag.ToString());
    
                    i++;
                }
    
                return MvcHtmlString.Create(builder.ToString());
            }
    
            public static MvcHtmlString RadioButtonList(this HtmlHelper helper, string name, object selectedValue, params string[] options)
            {
                var newOptions = new List<ListOption>();
    
                foreach (var option in options)
                {
                    newOptions.Add(new ListOption { Text = option, Value = option });
                }
    
                return helper.RadioButtonList(name, selectedValue, newOptions.ToArray());
            }
    
            public static MvcHtmlString YesNoRadioButtonList(this HtmlHelper helper, string name, object selectedValue)
            {
                return helper.RadioButtonList(name, selectedValue, "Yes", "No");
            }
        }

    second way is building helpers in current view page:

    @helper RadioButtonList(string name, object selectedValue, params ListOption[] options)
       {
           int i = 0;
           
           foreach (var option in options)
           {
               bool isChecked = selectedValue == null? false : selectedValue.ToString() == option.Value.ToString();           
               string id = name + i;
               @Html.RadioButton(name, option.Value, isChecked, new { id = id })
               <label for="@id"> @option.Text</label>
               i++;
           }       
       }
    
       @helper RadioButtonList(string name, object selectedValue, params string[] options)
       { 
           var newOptions = new List<ListOption>();
            
           foreach (var option in options)
           {
               newOptions.Add(new ListOption { Text = option, Value = option });
           }
    
           @RadioButtonList(name, selectedValue, newOptions.ToArray());
       }
    
       @helper YesNoRadioButtonList(string name, object selectedValue)
       {       
           @RadioButtonList(name, selectedValue, "Yes", "No");
       }

    the third way is building helpers in a view under App_Code folder:

    @using MVC = System.Web.Mvc;
    @using System.Web.Mvc.Html;
    @using MvcApplication8.Models;
    
    @helper RadioButtonList(MVC.HtmlHelper htmlHelper, string name, object selectedValue, params ListOption[] options)
    {
        int i = 0;
    
        foreach (var option in options)
        {
            bool isChecked = selectedValue == null ? false : selectedValue.ToString() == option.Value.ToString();
            string id = name + i;
            @htmlHelper.RadioButton(name, option.Value, isChecked, new { id = id })
            <label for="@id">@option.Text</label>
            i++;
        }       
    }
    
    @helper RadioButtonList(MVC.HtmlHelper htmlHelper, string name, object selectedValue, params string[] options)
    { 
        var newOptions = new List<ListOption>();
    
        foreach (var option in options)
        {
            newOptions.Add(new ListOption { Text = option, Value = option });
        }
    
        @RadioButtonList(htmlHelper, name, selectedValue, newOptions.ToArray());
    }
    
    @helper YesNoRadioButtonList(MVC.HtmlHelper htmlHelper, string name, object selectedValue)
    {       
        @RadioButtonList(htmlHelper, name, selectedValue, "Yes", "No");
    }

    how to use these helpers:

      @YesNoRadioButtonList("myo1", "")
        <br />
        @RadioButtonList("myo2", "", new ListOption[] { new ListOption { Text = "Yes", Value = 1 }, new ListOption { Text = "No", Value = 2 } })
        <br />
        @RadioButtonList("myo3", "", "A", "B", "C", "D")
        <hr />
        <br/>
        @Html.YesNoRadioButtonList("myo4", "")
        <br />
        @Html.RadioButtonList("myo5", "", new ListOption[] { new ListOption { Text = "Yes", Value = 1 }, new ListOption { Text = "No", Value = 2 } })
        <br />
        @Html.RadioButtonList("myo6", "", "A", "B", "C", "D")
        <hr />
        <br/>
        @CustomHelpers.YesNoRadioButtonList(Html,"myo7", "")
        <br />
        @CustomHelpers.RadioButtonList(Html, "myo8", "", new ListOption[] { new ListOption { Text = "Yes", Value = 1 }, new ListOption { Text = "No", Value = 2 } })
        <br />
        @CustomHelpers.RadioButtonList(Html, "myo9", "", "A", "B", "C", "D")
  • 相关阅读:
    vue 把后端返回的图片和url链接生成的二维码用canvas 合成一张图片
    Dart和JavaScript对比小结
    webgl学习,知识储备
    nightwatch+selenium做e2e自动化测试采坑小计
    linux centos7 环境变量设置
    ES6学习笔记
    SQLserver数据库还原语句
    AngularJs的那些坑(持续更新...)
    Hosting socket.io WebSocket apps in IIS using iisnode
    mongodb 数据库操作--备份 还原 导出 导入
  • 原文地址:https://www.cnblogs.com/shcity/p/2941187.html
Copyright © 2011-2022 走看看