私は次のように、あなたのラジオボタンリストをレンダリングするためのHtmlHelperを使用することをお勧めします:
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString RadioButtonListFor<TModel, TList, TSelectedItem>(
this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TList>> expression,
Expression<Func<TModel, TSelectedItem>> selectedItem)
{
return RadioButtonListFor(htmlHelper, expression, selectedItem, null /* htmlAttributes */);
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString RadioButtonListFor<TModel, TList, TSelectedItem>(
this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TList>> expression,
Expression<Func<TModel, TSelectedItem>> selectedItem, object htmlAttributes)
{
return RadioButtonListFor(htmlHelper, expression, selectedItem, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString RadioButtonListFor<TModel, TList, TSelectedItem>(
this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TList>> expression,
Expression<Func<TModel, TSelectedItem>> selectedItem, IDictionary<string, object> htmlAttributes)
{
if (expression == null)
{
throw new ArgumentNullException("expression");
}
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
IEnumerable<SelectListItem> items = null;
if (metadata.Model != null)
{
IEnumerable<SelectListItem> modelItems = (IEnumerable<SelectListItem>)metadata.Model;
if (modelItems != null)
{
items = modelItems;
}
}
ModelMetadata selectedItemMetadata = ModelMetadata.FromLambdaExpression(selectedItem, htmlHelper.ViewData);
return RadioButtonListHelper(htmlHelper, metadata, selectedItemMetadata, ExpressionHelper.GetExpressionText(selectedItem), items, htmlAttributes);
}
private static MvcHtmlString RadioButtonListHelper(HtmlHelper htmlHelper, ModelMetadata metadata,
ModelMetadata selectedItemMetadata, string name, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes)
{
// Verify arguments
if (String.IsNullOrEmpty(name)) throw new ArgumentNullException("name", "Name cannot be null");
if (selectList == null) throw new ArgumentNullException("selectList", "Select list cannot be null");
if (selectList.Count() < 1) throw new ArgumentException("Select list must contain at least one value", "selectList");
string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
string fullId = htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix + "_" + name;
IDictionary<string, object> validationAttributes = htmlHelper
.GetUnobtrusiveValidationAttributes(ExpressionHelper.GetExpressionText(name), selectedItemMetadata);
// Define items
StringBuilder items = new StringBuilder();
// Loop through items
Int32 index = 0;
foreach (SelectListItem i in selectList)
{
// Define check box input
TagBuilder input = new TagBuilder("input");
input.MergeAttribute("type", "radio");
input.MergeAttribute("name", fullName, true);
if (i.Selected)
input.MergeAttribute("checked", "checked");
input.MergeAttribute("value", i.Value);
if (index == 0)
input.MergeAttributes(validationAttributes);
input.MergeAttributes(htmlAttributes);
// Define label
TagBuilder label = new TagBuilder("label");
label.MergeAttribute("for", fullId + "[" + index.ToString() + "].Selected");
label.InnerHtml = i.Text;
// Add item
items.AppendFormat("\r\t<div>\r\t\t{0}\r\t\t{1}\r\t</div>",
input.ToString(TagRenderMode.Normal),
label.ToString(TagRenderMode.Normal));
index++;
}
// Return list
return new MvcHtmlString(items.ToString() + "\r");
}
MemberPackageListItem
はタイプIEnumerable<SelectListItem>
でなければならないことに注意してください。
@Html.RadioButtonListFor(m => m.MemberPackageListItem, m => m.SelectedPackageId)
counsellorben私は@ counsellorbenの答えの技術的な包括性を感謝し、将来の使用のために周りにそれを維持しますが、今日私はより迅速かつないに到着