2017-01-24 6 views
-1

私は、特定のユーザの利用可能なアドレスのリストを生成するのに必要なビューモデルを用意しています。
私は意図した通りに構造内のアドレスを表示することができます。MVC5でラジオボタンリストを生成

@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"/> 

私はこれを実現するにはどうすればよいですか?

+2

ここには部分的な理由がありますか?あなたのループで 'RadioButtonFor()'を使わないのはなぜですか? –

+0

この同じアドレスビューは、同じページの異なるセクションで異なるモデルで繰り返されます。請求先住所や配送先住所など – GilliVilla

+1

次に、部分的なものではなく、「EditorTemplate」を使用する必要があります(バインディングの正しい名前属性を取得するため)。また、ビュー内のモデルには、選択した値にバインドするための 'addressId'プロパティがありますか?そして、あなたがビューに表示したいAddressモデルの他のプロパティは何ですか? –

答えて

0

ここでRadioButtonForを探しているのは、ブートストラップを備えた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属性が必要な場合に備えて、それらを直接渡して、最終的なパラメータとして渡します。 このヘルプをご希望の場合

+0

正しい2ウェイモデルのバインディングを使用せず、クライアント側の検証もしていない恐ろしい解決方法 - これは 'HtmlHelper'メソッドの仕組みを理解していないことを示しています(質問に対処しません)。 –

+0

@StephenMuecke、あなたが値を送ることができるIDとそれは入力タイプ= "ラジオ"を正しく生成するでしょう、なぜこの拡張メソッドが正しく動作しないのかを最初に説明できますか? HtmlHelpersは事前定義されたコードを生成します。たとえば、Html.Rawは新しいHtmlString(値)を返します。 2番目の例では、Html.LabelForはメタデータと共に、最後にtagBuilder.ToMvcHtmlStringを呼び出すTagBuilderを作成します。ここであなたのポイントは何ですか?ヘルパーメソッドで正しいhtmlを生成していませんか? – Zinov

+0

コメントには2つの理由があります。これは、ModelState(存在する場合)の値を使用して正しい2-wayモデルのバインドを行うことはありません。ここで、クライアント側の検証に必要な 'data-val- *'属性が生成されます。 –