本文介绍了有没有人实施RadioButtonListFor< T> ASP.NET MVC的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有在ASP.NET MVC期货的 Html.RadioButtonList
扩展方法。有没有人发现了一个code的强类型版本 RadioButtonListFor< T>
。它看起来像这样一个观点:
<%= Html.RadioButtonListFor(型号=> model.Item,Model.ItemList)%GT;
解决方案
下面是aspx页面使用
<%= Html.RadioButtonListFor(M = GT; m.GenderRadioButtonList)%GT;
下面是视图模型
公共类HomePageViewModel
{
公共枚举GenderType
{
男,
女
}
公共RadioButtonListViewModel< GenderType> GenderRadioButtonList {搞定;组; } 公共HomePageViewModel()
{
GenderRadioButtonList =新RadioButtonListViewModel< GenderType>
{
ID =性别,
的SelectedValue = GenderType.Male,
ListItems的=新名单< RadioButtonListItem< GenderType>>
{
新RadioButtonListItem< GenderType>的{text =男,值= GenderType.Male},
新RadioButtonListItem< GenderType>的{text =女,值= GenderType.Female}
}
};
}
}
下面是用于单选按钮列表视图模型
公共类RadioButtonListViewModel< T>
{
公共字符串ID {搞定;组; }
私人ŧ了selectedValue;
公共ŧ的SelectedValue
{
{返回了selectedValue; }
组
{
了selectedValue =价值;
UpdatedSelectedItems();
}
} 私人无效UpdatedSelectedItems()
{
如果(ListItems的== NULL)
返回; ListItems.ForEach(李= GT; li.Selected =等于(li.Value,的SelectedValue));
} 私人列表< RadioButtonListItem< T>> listItems中;
公开名单< RadioButtonListItem< T>> ListItems的
{
{返回时listItems; }
组
{
时listItems =价值;
UpdatedSelectedItems();
}
}
}公共类RadioButtonListItem< T>
{
公共BOOL选择{搞定;组; } 公共字符串文本{搞定;组; } 公众的T值{搞定;组; } 公共重写字符串的ToString()
{
返回Value.ToString();
}
}
下面是RadioButtonListFor扩展方法
公共静态类HtmlHelperExtensions
{
公共静态字符串RadioButtonListFor<的TModel,TRadioButtonListValue>(此的HtmlHelper<的TModel>的HtmlHelper,防爆pression< Func键<的TModel,RadioButtonListViewModel< TRadioButtonListValue>>>前pression)其中的TModel:类
{
返回htmlHelper.RadioButtonListFor(如pression,NULL);
} 公共静态字符串RadioButtonListFor<的TModel,TRadioButtonListValue>(此的HtmlHelper<的TModel>的HtmlHelper,防爆pression< Func键<的TModel,RadioButtonListViewModel< TRadioButtonListValue>>>前pression,对象htmlAttributes)其中的TModel:类
{
返回htmlHelper.RadioButtonListFor(如pression,新RouteValueDictionary(htmlAttributes));
} 公共静态字符串RadioButtonListFor<的TModel,TRadioButtonListValue>(此的HtmlHelper<的TModel>的HtmlHelper,防爆pression< Func键<的TModel,RadioButtonListViewModel< TRadioButtonListValue>>>前pression,IDictionary的<字符串对象> htmlAttributes),其中的TModel:类
{
VAR inputName = GetInputName(如pression); RadioButtonListViewModel< TRadioButtonListValue>单选按钮列表=的GetValue(的HtmlHelper,前pression); 如果(单选按钮列表== NULL)
返回的String.Empty; 如果(radioButtonList.ListItems == NULL)
返回的String.Empty; VAR divTag =新TagBuilder(分区);
divTag.MergeAttribute(ID,inputName);
divTag.MergeAttribute(类,无线);
的foreach(在radioButtonList.ListItems VAR项)
{
VAR radioButtonTag =单选按钮(的HtmlHelper,inputName,新SelectListItem的{text = item.Text,选择= item.Selected,值= item.Value.ToString()},htmlAttributes); divTag.InnerHtml + = radioButtonTag;
} 返回divTag + htmlHelper.ValidationMessage(inputName,*);
} 公共静态字符串GetInputName<的TModel,TProperty>(防爆pression<&Func键LT;的TModel,TProperty>>前pression)
{
如果(如pression.Body.NodeType ==前pressionType.Call)
{
VAR methodCallEx pression =(MethodCallEx pression)前pression.Body;
字符串名称= GetInputName(methodCallEx pression);
返回name.Substring(例如pression.Parameters [0] .Name.Length + 1); }
返回前pression.Body.ToString()子串(如pression.Parameters [0] .Name.Length + 1)。
} 私人静态字符串GetInputName(MethodCallEx pression前pression)
{
// P => 。p.Foo.Bar()Baz.ToString()=> p.Foo或抛出... VAR methodCallEx pression =前pression.Object作为MethodCallEx pression;
如果(methodCallEx pression!= NULL)
{
返回GetInputName(methodCallEx pression);
}
返回前pression.Object.ToString();
} 公共静态字符串单选按钮(这的HtmlHelper的HtmlHelper,字符串名称,SelectListItem的listItem,
IDictionary的<字符串对象> htmlAttributes)
{
VAR inputIdSb =新的StringBuilder();
inputIdSb.Append(名)
。附加(_)
.Append(listItem.Value); VAR SB =新的StringBuilder(); VAR建设者=新TagBuilder(输入);
如果(listItem.Selected)builder.MergeAttribute(选中,检查);
builder.MergeAttribute(类型,无线电);
builder.MergeAttribute(价值,listItem.Value);
builder.MergeAttribute(ID,inputIdSb.ToString());
builder.MergeAttribute(名,名称+.SelectedValue);
builder.MergeAttributes(htmlAttributes);
sb.Append(builder.ToString(TagRenderMode.SelfClosing));
sb.Append(RadioButtonLabel(inputIdSb.ToString(),listItem.Text,htmlAttributes));
sb.Append(< BR>中); 返回sb.ToString();
} 公共静态字符串RadioButtonLabel(字符串inputId,串displayText,
IDictionary的<字符串对象> htmlAttributes)
{
VAR labelBuilder =新TagBuilder(标签);
labelBuilder.MergeAttribute(代表,inputId);
labelBuilder.MergeAttributes(htmlAttributes);
labelBuilder.InnerHtml = displayText; 返回labelBuilder.ToString(TagRenderMode.Normal);
}
公共静态TProperty的GetValue<的TModel,TProperty>(的HtmlHelper<的TModel>的HtmlHelper,防爆pression< Func键<的TModel,TProperty>>前pression)其中的TModel:类
{
的TModel模型= htmlHelper.ViewData.Model;
如果(型号== NULL)
{
返回默认值(TProperty);
}
FUNC<的TModel,TProperty> FUNC =前pression.Compile();
返回FUNC(模型);
}
}
There was an Html.RadioButtonList
extension method in ASP.NET MVC Futures. Has anyone found a code for a strongly typed version RadioButtonListFor<T>
. It would look like this in a view:
<%= Html.RadioButtonListFor(model=>model.Item,Model.ItemList) %>
解决方案
Here is the usage in the aspx page
<%= Html.RadioButtonListFor(m => m.GenderRadioButtonList)%>
Here is the view model
public class HomePageViewModel
{
public enum GenderType
{
Male,
Female
}
public RadioButtonListViewModel<GenderType> GenderRadioButtonList { get; set; }
public HomePageViewModel()
{
GenderRadioButtonList = new RadioButtonListViewModel<GenderType>
{
Id = "Gender",
SelectedValue = GenderType.Male,
ListItems = new List<RadioButtonListItem<GenderType>>
{
new RadioButtonListItem<GenderType>{Text = "Male", Value = GenderType.Male},
new RadioButtonListItem<GenderType>{Text = "Female", Value = GenderType.Female}
}
};
}
}
Here's the view model used for radio button lists
public class RadioButtonListViewModel<T>
{
public string Id { get; set; }
private T selectedValue;
public T SelectedValue
{
get { return selectedValue; }
set
{
selectedValue = value;
UpdatedSelectedItems();
}
}
private void UpdatedSelectedItems()
{
if (ListItems == null)
return;
ListItems.ForEach(li => li.Selected = Equals(li.Value, SelectedValue));
}
private List<RadioButtonListItem<T>> listItems;
public List<RadioButtonListItem<T>> ListItems
{
get { return listItems; }
set
{
listItems = value;
UpdatedSelectedItems();
}
}
}
public class RadioButtonListItem<T>
{
public bool Selected { get; set; }
public string Text { get; set; }
public T Value { get; set; }
public override string ToString()
{
return Value.ToString();
}
}
Here's the extension methods for RadioButtonListFor
public static class HtmlHelperExtensions
{
public static string RadioButtonListFor<TModel, TRadioButtonListValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel<TRadioButtonListValue>>> expression) where TModel : class
{
return htmlHelper.RadioButtonListFor(expression, null);
}
public static string RadioButtonListFor<TModel, TRadioButtonListValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel<TRadioButtonListValue>>> expression, object htmlAttributes) where TModel : class
{
return htmlHelper.RadioButtonListFor(expression, new RouteValueDictionary(htmlAttributes));
}
public static string RadioButtonListFor<TModel, TRadioButtonListValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel<TRadioButtonListValue>>> expression, IDictionary<string, object> htmlAttributes) where TModel : class
{
var inputName = GetInputName(expression);
RadioButtonListViewModel<TRadioButtonListValue> radioButtonList = GetValue(htmlHelper, expression);
if (radioButtonList == null)
return String.Empty;
if (radioButtonList.ListItems == null)
return String.Empty;
var divTag = new TagBuilder("div");
divTag.MergeAttribute("id", inputName);
divTag.MergeAttribute("class", "radio");
foreach (var item in radioButtonList.ListItems)
{
var radioButtonTag = RadioButton(htmlHelper, inputName, new SelectListItem{Text=item.Text, Selected = item.Selected, Value = item.Value.ToString()}, htmlAttributes);
divTag.InnerHtml += radioButtonTag;
}
return divTag + htmlHelper.ValidationMessage(inputName, "*");
}
public static string GetInputName<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
{
if (expression.Body.NodeType == ExpressionType.Call)
{
var methodCallExpression = (MethodCallExpression)expression.Body;
string name = GetInputName(methodCallExpression);
return name.Substring(expression.Parameters[0].Name.Length + 1);
}
return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1);
}
private static string GetInputName(MethodCallExpression expression)
{
// p => p.Foo.Bar().Baz.ToString() => p.Foo OR throw...
var methodCallExpression = expression.Object as MethodCallExpression;
if (methodCallExpression != null)
{
return GetInputName(methodCallExpression);
}
return expression.Object.ToString();
}
public static string RadioButton(this HtmlHelper htmlHelper, string name, SelectListItem listItem,
IDictionary<string, object> htmlAttributes)
{
var inputIdSb = new StringBuilder();
inputIdSb.Append(name)
.Append("_")
.Append(listItem.Value);
var sb = new StringBuilder();
var builder = new TagBuilder("input");
if (listItem.Selected) builder.MergeAttribute("checked", "checked");
builder.MergeAttribute("type", "radio");
builder.MergeAttribute("value", listItem.Value);
builder.MergeAttribute("id", inputIdSb.ToString());
builder.MergeAttribute("name", name + ".SelectedValue");
builder.MergeAttributes(htmlAttributes);
sb.Append(builder.ToString(TagRenderMode.SelfClosing));
sb.Append(RadioButtonLabel(inputIdSb.ToString(), listItem.Text, htmlAttributes));
sb.Append("<br>");
return sb.ToString();
}
public static string RadioButtonLabel(string inputId, string displayText,
IDictionary<string, object> htmlAttributes)
{
var labelBuilder = new TagBuilder("label");
labelBuilder.MergeAttribute("for", inputId);
labelBuilder.MergeAttributes(htmlAttributes);
labelBuilder.InnerHtml = displayText;
return labelBuilder.ToString(TagRenderMode.Normal);
}
public static TProperty GetValue<TModel, TProperty>(HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) where TModel : class
{
TModel model = htmlHelper.ViewData.Model;
if (model == null)
{
return default(TProperty);
}
Func<TModel, TProperty> func = expression.Compile();
return func(model);
}
}
这篇关于有没有人实施RadioButtonListFor&LT; T&GT; ASP.NET MVC的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!