Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC5中生成单选按钮列表_C#_Html_.net_Asp.net Mvc_Asp.net Mvc 5 - Fatal编程技术网

C# 在MVC5中生成单选按钮列表

C# 在MVC5中生成单选按钮列表,c#,html,.net,asp.net-mvc,asp.net-mvc-5,C#,Html,.net,Asp.net Mvc,Asp.net Mvc 5,我有必要的视图模型来生成给定用户的可用地址列表。 我也能够按照我的意愿在结构中显示地址 @if (Model.Addresses.Any()) { <ul class="ol"> @foreach (var address in Model.Addresses) { <li> @{ Html.RenderPartial("_Address", address); }

我有必要的视图模型来生成给定用户的可用地址列表。
我也能够按照我的意愿在结构中显示地址

@if (Model.Addresses.Any())
{
    <ul class="ol">
        @foreach (var address in Model.Addresses)
        {
            <li>
                @{ Html.RenderPartial("_Address", address); }
            </li>
        }
    </ul>
}

如何实现这一点?

如果您正在寻找RadioButton,以下是带有引导功能的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
#区域引导/HTML 5单选按钮
/// 
///引导和HTML5单选按钮。
/// 
///此方法扩展的HTML帮助程序实例。
///一个表达式,用于标识包含要渲染的属性的对象。
///要设置的“id”属性名称。
///要设置的“name”属性。
///要在此单选按钮旁边显示的文本。
///要设置的“值”属性。
///包含要为元素设置的HTML属性的对象。
///设置了相应类型的HTML单选按钮。
公共静态MvcHtmlString RadioButtonBootstrapFor(
这个HtmlHelper HtmlHelper,
表情表情,
字符串id,
字符串名,
字符串文本,
字符串值,
对象htmlAttributes=null)
{
返回RadioButtonBootTrapfor(htmlHelper、表达式、id、名称、文本、值、false、false、false、htmlAttributes);
}
/// 
///按钮助手中的引导和HTML5单选按钮。
///此帮助程序假定您已添加适当的CSS来设置此单选按钮的样式。
/// 
///此方法扩展的HTML帮助程序实例。
///一个表达式,用于标识包含要渲染的属性的对象。
///要设置的“id”属性名称。
///要设置的“name”属性。
///要在此单选按钮旁边显示的文本。
///要设置的“值”属性。
///是否在此单选按钮上设置“已选中”属性。
///是否在此单选按钮上设置“自动聚焦”属性。
///是否对引导类使用“radio inline”。
///包含要为元素设置的HTML属性的对象。
///设置了相应类型的HTML单选按钮。
公共静态MvcHtmlString RadioButtonBootstrapFor(
这个HtmlHelper HtmlHelper,
表情表情,
字符串id,
字符串名,
字符串文本,
字符串值,
布尔被检查过,
布尔是自动聚焦,
bool useInline=false,
对象htmlAttributes=null)
{
StringBuilder sb=新的StringBuilder(512);
string htmlChecked=string.Empty;
string htmlAutoFocus=string.Empty;
字符串rdoClass=“radio”;
如果(已检查)
{
htmlChecked=“checked='checked'”;
}
如果(自动聚焦)
{
htmlAutoFocus=“autofocus='autofocus'”;
}
如果(使用内联)
{
rdoClass=“无线电内联”;
}
//构建单选按钮
sb.AppendFormat(“”,rdoClass);
某人加上(“”);
sb.追加格式(“,
id、名称、值、htmlChecked、htmlAutoFocus、,
GetHtmlatAttributes(HtmlatAttributes));
sb.附录格式(“{0}”,文本);
某人加上(“”);
某人加上(“”);
//返回一个MVC HTML字符串
返回MvcHtmlString.Create(sb.ToString());
}
#端区
现在,正如您看到的id一样,名称和值都是参数。直接传递它们,如果需要其他html属性,请将它们作为最终参数传递。
希望获得此帮助

您在此处使用分部代码是否有特殊原因?为什么不在循环中使用
RadioButton()
?相同的地址视图…将在同一页面的不同部分以不同的型号重复出现。如账单地址和发货地址等。那么您应该使用
编辑模板
,而不是部分模板(以便获得正确的绑定名称属性)。视图中的模型是否具有用于绑定到选定值的属性
addressId
?那么,您希望在视图中显示的地址模型的其他属性是什么?难道您不能简单地将RadioButton放在分部中吗?您需要帮助制作的RadioButton吗?@StephenMuecke是的,该模型具有属性
addressId
,我需要的其他属性是Name、Address1、Address2、State、PostalCode。。。。我确实能够在视图中显示。问题在于如何在这些地址前面加上单选按钮,每个单选按钮都带有问题中提出的id和值。这是一个糟糕的解决方案,它甚至没有使用正确的双向模型绑定,也没有客户端验证-这表明不了解
HtmlHelper
方法是如何工作的(它没有解决问题)@StephenMuecke,一旦您有了id,您就可以发送值,它将正确地生成输入type=“radio”,请您首先解释一下为什么这个扩展方法不能正常工作?HtmlHelpers生成预定义的代码,例如Html.Raw make返回一个新的HtmlString(值)。第二个示例Html.LabelFor与元数据一起创建一个标记生成器,该标记生成器最后调用TagBuilder.ToMvcHtmlString。你的观点是什么?是否使用Helper方法生成正确的html?在注释中有很多原因需要解释。没有任何地方可以使用
ModelState
(如果存在)中的值进行正确的双向模型绑定。现在,这将在何处生成客户端验证所需的
data val-*
属性。我不同意第二点,data val-属性可以是最后一个参数的一部分,但它可以作为帮助器的一部分在内部处理,第一点您是对的。但是使用这些方法,您仍然可以生成上面的html,那么您也不知道如何创建扩展方法。如果你想写自己的代码,我建议你开始研究源代码。代码中的所有内容都可以简单地
#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