惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
Y
Y Combinator Blog
Recorded Future
Recorded Future
博客园 - Franky
I
InfoQ
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
Cloudbric
Cloudbric
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Check Point Blog
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
U
Unit 42
N
Netflix TechBlog - Medium
The Cloudflare Blog
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
T
Troy Hunt's Blog
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
B
Blog
S
Securelist
H
Hacker News: Front Page
Google Online Security Blog
Google Online Security Blog
G
Google Developers Blog

博客园 - shcity

Troubleshooting on TransactionScope Parse string to JSON object Parse date in js blockUI doesn't close when download file in asp.net define namespace in JS Dense_Rank(), Row_Number(), Rank() in sql server reading and writing variable through lock in SSIS script task Fix the issue that cannot open SSIS in BIDS using JavaScriptSerializer to serialize object to json using ISerializable to control serialization and deserialization ViewStateAutoManager ReportingService formatting Build my own DataTable split a string into an array through comma div with separated html template - shcity 正则表达式替换日期 半透明的div对话框 foreach 的自动转化类型 在Ajax1.0中调用页面CS文件中的方法
three ways creating custom helpers to show RadioButtonList in MVC
shcity · 2013-03-03 · via 博客园 - shcity

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")