Mvc 3 中 有RadioButtonFor 但是没有list,扩展一下,方法如下:

   

   

public static MvcHtmlString RadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<stringobject> 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<string> values = from object value in defaultValues select Convert.ToString(value, CultureInfo.CurrentCulture);
                HashSet<string> selectedValues = new HashSet<string>(values, StringComparer.OrdinalIgnoreCase);
                List<SelectListItem> newSelectList = new List<SelectListItem>();

                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());
        }

相关文章:

  • 2021-09-17
  • 2021-05-22
  • 2021-12-08
  • 2021-08-16
  • 2021-03-08
  • 2021-06-26
  • 2022-01-09
猜你喜欢
  • 2021-08-08
  • 2022-12-23
  • 2021-06-08
  • 2022-12-23
  • 2021-12-30
  • 2021-07-08
相关资源
相似解决方案