我有必要的视图模型来生成给定用户的可用地址列表 我也能够按照我的意图显示结构中的地址。
@if (Model.Addresses.Any())
{
<ul class="ol">
@foreach (var address in Model.Addresses)
{
<li>
@{ Html.RenderPartial("_Address", address); }
</li>
}
</ul>
}
我打算让每个地址都以单选按钮为前缀。
我正在渲染的模型给了我一个id
。 (说 123456)
因此,每个项目的单选按钮应具有以下示例中的这些属性 -
<input type="radio" id="addressId_123456" name="addressId" value="123456"/>
我如何做到这一点?
答案 0 :(得分:0)
如果您正在寻找RadioButtonFor,这里是带有bootstrap的HTML帮助程序的代码。感谢@PaulDSheriff
#region Bootstrap/HTML 5 Radio Button
/// <summary>
/// Bootstrap and HTML 5 Radio Button.
/// </summary>
/// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
/// <param name="expression">An expression that identifies the object that contains the properties to render.</param>
/// <param name="id">The 'id' attribute name to set.</param>
/// <param name="name">The 'name' attribute to set.</param>
/// <param name="text">The text to display next to this radio button.</param>
/// <param name="value">The 'value' attribute to set.</param>
/// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element.</param>
/// <returns>An HTML radio button with the appropriate type set.</returns>
public static MvcHtmlString RadioButtonBootstrapFor<TModel, TValue>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TValue>> expression,
string id,
string name,
string text,
string value,
object htmlAttributes = null)
{
return RadioButtonBootstrapFor(htmlHelper, expression, id, name, text, value, false, false, false, htmlAttributes);
}
/// <summary>
/// Bootstrap and HTML 5 Radio Button in a Button Helper.
/// This helper assumes you have added the appropriate CSS to style this radio button.
/// </summary>
/// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
/// <param name="expression">An expression that identifies the object that contains the properties to render.</param>
/// <param name="id">The 'id' attribute name to set.</param>
/// <param name="name">The 'name' attribute to set.</param>
/// <param name="text">The text to display next to this radio button.</param>
/// <param name="value">The 'value' attribute to set.</param>
/// <param name="isChecked">Whether or not to set the 'checked' attribute on this radio button.</param>
/// <param name="isAutoFocus">Whether or not to set the 'autofocus' attribute on this radio button.</param>
/// <param name="useInline">Whether or not to use 'radio-inline' for the Bootstrap class.</param>
/// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element.</param>
/// <returns>An HTML radio button with the appropriate type set.</returns>
public static MvcHtmlString RadioButtonBootstrapFor<TModel, TValue>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TValue>> expression,
string id,
string name,
string text,
string value,
bool isChecked,
bool isAutoFocus,
bool useInline = false,
object htmlAttributes = null)
{
StringBuilder sb = new StringBuilder(512);
string htmlChecked = string.Empty;
string htmlAutoFocus = string.Empty;
string rdoClass = "radio";
if (isChecked)
{
htmlChecked = "checked='checked'";
}
if (isAutoFocus)
{
htmlAutoFocus = "autofocus='autofocus'";
}
if (useInline)
{
rdoClass = "radio-inline";
}
// Build the Radio Button
sb.AppendFormat("<div class='{0}'>", rdoClass);
sb.Append(" <label>");
sb.AppendFormat(" <input id='{0}' name='{1}' type='radio' value='{2}' {3} {4} {5} />",
id, name, value, htmlChecked, htmlAutoFocus,
GetHtmlAttributes(htmlAttributes));
sb.AppendFormat(" {0}", text);
sb.Append(" </label>");
sb.Append("</div>");
// Return an MVC HTML String
return MvcHtmlString.Create(sb.ToString());
}
#endregion
现在,当您看到id时,名称和值是参数。直接传递它们,以防您需要其他html属性,将它们作为最终参数传递。 希望这个帮助