博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用户控件 RadioButtonList
阅读量:4572 次
发布时间:2019-06-08

本文共 10940 字,大约阅读时间需要 36 分钟。

public static MvcHtmlString RadioButtonList(this HtmlHelper htmlHelper, string name, string codeCategory, RepeatDirection repeatDirection = RepeatDirection.Horizontal)        {            var codes = CodeManager.GetCodes(codeCategory);            return ListControlUtil.GenerateHtml(name, codes, repeatDirection, "radio", null);        }        public static MvcHtmlString RadioButtonListFor
(this HtmlHelper
htmlHelper, Expression
> expression, Collection
codeCategory,CodeDescription selectCodeCategory, RepeatDirection repeatDirection = RepeatDirection.Horizontal) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression
(expression, htmlHelper.ViewData); string name = ExpressionHelper.GetExpressionText(expression); string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); return ListControlUtil.GenerateHtml(fullHtmlFieldName, codeCategory, selectCodeCategory, repeatDirection, "radio", metadata.Model); } public static MvcHtmlString RadioButtonListFor
(this HtmlHelper
htmlHelper, Expression
> expression, Collection
codeCategory, RepeatDirection repeatDirection = RepeatDirection.Horizontal) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression
(expression, htmlHelper.ViewData); string name = ExpressionHelper.GetExpressionText(expression); string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); return ListControlUtil.GenerateHtml(fullHtmlFieldName, codeCategory, repeatDirection, "radio", metadata.Model); } public static MvcHtmlString RadioButtonListForOne
(this HtmlHelper
htmlHelper, Expression
> expression, IEnumerable
selectList, string optionLabel, IDictionary
htmlAttributes) { if (expression == null) { throw new ArgumentNullException("expression"); } string name = ExpressionHelper.GetExpressionText(expression); string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); if (String.IsNullOrEmpty(fullName)) { throw new ArgumentException("name"); } bool usedViewData = false; // If we got a null selectList, try to use ViewData to get the list of items. if (selectList == null) { selectList = htmlHelper.GetSelectData(fullName); usedViewData = true; } object defaultValue = htmlHelper.GetModelStateValue(fullName, typeof(string)); // If we haven't already used ViewData to get the entire list of items then we need to // use the ViewData-supplied value before using the parameter-supplied value. if (!usedViewData) { if (defaultValue == null) { defaultValue = htmlHelper.ViewData.Eval(fullName); } } if (defaultValue != null) { IEnumerable defaultValues = new[] { defaultValue }; IEnumerable
values = from object value in defaultValues select Convert.ToString(value, CultureInfo.CurrentCulture); HashSet
selectedValues = new HashSet
(values, StringComparer.OrdinalIgnoreCase); List
newSelectList = new List
(); foreach (SelectListItem item in selectList) { item.Selected = (item.Value != null) ? selectedValues.Contains(item.Value) : selectedValues.Contains(item.Text); newSelectList.Add(item); } selectList = newSelectList; } #region TagBuilder table = new TagBuilder("table"); int i = 0; foreach (SelectListItem item in selectList) { // TagBuilder tr = new TagBuilder("tr"); i++; string id = string.Format("{0}_{1}", name, i); TagBuilder td = new TagBuilder("td"); td.InnerHtml = GenerateRadioHtml(name, id, item.Text, item.Value, item.Selected, htmlAttributes); tr.InnerHtml = td.ToString(); table.InnerHtml += tr.ToString(); } #endregion return new MvcHtmlString(table.ToString()); } private static IEnumerable
GetSelectData(this HtmlHelper htmlHelper, string name) { object o = null; if (htmlHelper.ViewData != null) { o = htmlHelper.ViewData.Eval(name); } if (o == null) { throw new InvalidOperationException( "IEnumerable
"); } IEnumerable
selectList = o as IEnumerable
; if (selectList == null) { throw new InvalidOperationException( "IEnumerable
"); } return selectList; } private static object GetModelStateValue(this HtmlHelper htmlHelper, string key, Type destinationType) { ModelState modelState; if (htmlHelper.ViewData.ModelState.TryGetValue(key, out modelState)) { if (modelState.Value != null) { return modelState.Value.ConvertTo(destinationType, null /* culture */); } } return null; } private static string GenerateRadioHtml(string name, string id, string labelText, string value, bool isChecked, IDictionary
htmlAttributes) { StringBuilder sb = new StringBuilder(); TagBuilder label = new TagBuilder("label"); label.MergeAttribute("for", id); label.SetInnerText(labelText); TagBuilder input = new TagBuilder("input"); input.GenerateId(id); input.MergeAttribute("name", name); input.MergeAttribute("type", "radio"); input.MergeAttribute("value", value); input.MergeAttributes(htmlAttributes); if (isChecked) { input.MergeAttribute("checked", "checked"); } sb.AppendLine(input.ToString()); sb.AppendLine(label.ToString()); return sb.ToString(); }

引用

public static class ListControlUtil    {        public static MvcHtmlString GenerateHtml(string name, Collection
codes, RepeatDirection repeatDirection, string type, object stateValue) { TagBuilder table = new TagBuilder("table"); int i = 0; bool isCheckBox = type == "checkbox"; if (repeatDirection == RepeatDirection.Horizontal) { TagBuilder tr = new TagBuilder("tr"); if (codes == null) { return null; } foreach (var code in codes) { i++; string id = string.Format("{0}_{1}", name, i); TagBuilder td = new TagBuilder("td"); bool isChecked = false; if (isCheckBox) { IEnumerable
currentValues = stateValue as IEnumerable
; isChecked = (null != currentValues && currentValues.Contains(code.Code)); } else { string currentValue = stateValue.ToString(); isChecked = (null != currentValue && code.Code == currentValue); } td.InnerHtml = GenerateRadioHtml(name, id, code.Description, code.Code, isChecked, type); tr.InnerHtml += td.ToString(); } table.InnerHtml = tr.ToString(); } else { foreach (var code in codes) { TagBuilder tr = new TagBuilder("tr"); i++; string id = string.Format("{0}_{1}", name, i); TagBuilder td = new TagBuilder("td"); bool isChecked = false; if (isCheckBox) { IEnumerable
currentValues = stateValue as IEnumerable
; isChecked = (null != currentValues && currentValues.Contains(code.Code)); } else { string currentValue = stateValue as string; isChecked = (null != currentValue && code.Code == currentValue); } td.InnerHtml = GenerateRadioHtml(name, id, code.Description, code.Code, isChecked, type); tr.InnerHtml = td.ToString(); table.InnerHtml += tr.ToString(); } } return new MvcHtmlString(table.ToString()); } public static MvcHtmlString GenerateHtml(string name, Collection
codes, CodeDescription selectCodeCategory, RepeatDirection repeatDirection, string type, object stateValue) { TagBuilder table = new TagBuilder("table"); int i = 0; bool isCheckBox = type == "checkbox"; if (repeatDirection == RepeatDirection.Horizontal) { TagBuilder tr = new TagBuilder("tr"); foreach (var code in codes) { i++; string id = string.Format("{0}_{1}", name, i); TagBuilder td = new TagBuilder("td"); bool isChecked = false; if (isCheckBox) { IEnumerable
currentValues = stateValue as IEnumerable
; isChecked = (null != currentValues && currentValues.Contains(code.Code)); } else { string currentValue = selectCodeCategory.Code; isChecked = (null != currentValue && code.Code == currentValue); } td.InnerHtml = GenerateRadioHtml(name, id, code.Description, code.Code, isChecked, type); tr.InnerHtml += td.ToString(); } table.InnerHtml = tr.ToString(); } else { foreach (var code in codes) { TagBuilder tr = new TagBuilder("tr"); i++; string id = string.Format("{0}_{1}", name, i); TagBuilder td = new TagBuilder("td"); bool isChecked = false; if (isCheckBox) { IEnumerable
currentValues = stateValue as IEnumerable
; isChecked = (null != currentValues && currentValues.Contains(code.Code)); } else { string currentValue = selectCodeCategory.Code; isChecked = (null != currentValue && code.Code == currentValue); } td.InnerHtml = GenerateRadioHtml(name, id, code.Description, code.Code, isChecked, type); tr.InnerHtml = td.ToString(); table.InnerHtml += tr.ToString(); } } return new MvcHtmlString(table.ToString()); } private static string GenerateRadioHtml(string name, string id, string labelText, string value, bool isChecked, string type) { StringBuilder sb = new StringBuilder(); TagBuilder label = new TagBuilder("label"); label.MergeAttribute("for", id); label.SetInnerText(labelText); TagBuilder input = new TagBuilder("input"); input.GenerateId(id); input.MergeAttribute("name", name); input.MergeAttribute("type", type); input.MergeAttribute("value", value); if (isChecked) { input.MergeAttribute("checked", "checked"); } sb.AppendLine(input.ToString()); sb.AppendLine(label.ToString()); return sb.ToString(); } }

控件所用自定义类CodeDescription

public class CodeDescription    {        public string Code { get; set; }        public string Description { get; set; }        public string Category { get; set; }        public CodeDescription(string code, string description, string category)        {            this.Code = code;            this.Description = description;            this.Category = category;        }    }

使用方法初始化Controller

Collection
listSex = new Collection
(); listSex.Add(new CodeDescription( "1", "男", "Sex" )); listSex.Add(new CodeDescription("0", "女", "Sex")); ViewBag.SexItems = listSex; model.SexItems = listSex; model.SexSeltectItem = new CodeDescription("1", "男", "Sex");

前端使用

@Html.RadioButtonListFor(c => c.Sex, Model.SexItems,Model.SexSeltectItem)@Html.ValidationMessageFor(model => model.Sex)

控制器获取

obj.Sex = model.Sex;

 

转载于:https://www.cnblogs.com/LiYiPeng2015/p/4987592.html

你可能感兴趣的文章
redis 学习(8)-- redis 客户端 -- Jedis
查看>>
正则表达式
查看>>
bzoj1385 [Baltic2000]Division expression
查看>>
字符的读入问题
查看>>
五子棋计算思路
查看>>
83. 移除已排序链表中重复的节点 Remove Duplicates from Sorted List
查看>>
linux的联网以及语言的更改
查看>>
145-PHP 使用<<<和HTML混编(一)
查看>>
栈的顺序存储结构以及实现
查看>>
【python】-- Socket粘包问题 ,解决粘包的几种方法、socket文件下载,md5值检验
查看>>
2016-09-12
查看>>
CDHD驱动器——ServoStudio配置高创伺服速度模式不转
查看>>
完整版本的停车场管理系统源代码带服务端+手机android客户端
查看>>
【UOJ 92】有向图的强联通分量
查看>>
bzoj 1192
查看>>
Windows10/Servers 2016的TrustedInstaller权限获取(及乱改System后救砖
查看>>
关于mysql转移数据库时没有导出sql脚本的情况下,如何导入数据到新的数据库中...
查看>>
链表逆序
查看>>
[zz]链表倒序
查看>>
简单易用的图像解码库介绍 —— stb_image
查看>>