如何在@ Html.RadioButtonFor

如果有:

<radio class="js-payment-method js-debit-card" type="button" title="Credit/Debit Card"><img src="~/_common/img/payment-options/card.png"><span>Credit / Debit Card</span></radio>

我得到了想要的结果。但是,我需要将此附加到我的模型上。所以从以下开始:

@Html.RadioButtonFor(model => model.PaymentMethodSelected,Enums.PaymentMethod.Card,new { @class = "radio__input js-payment-method",@type = "button",@title="Credit/Debit Card" })

我如何修改此图像以获取图像和跨度?

使用下面的@the_lotus建议,代码呈现为:

<label class="js-payment-method js-debit-card" type="button" title="Credit/Debit Card">
    <input checked="checked" class="js-payment-method" data-val="true" data-val-required="The PaymentMethodSelected field is required." id="PaymentMethodSelected" name="PaymentMethodSelected" type="button" value="Card"> 
   <img src="/_common/img/payment-options/card.png">
   <span>Credit / Debit Card</span>
</label>

所以现在的问题是,显示输入元素中的值'card',因为如果我将其设置为radio时,它是type =“ button”,那么该值消失了,但是我得到了单选按钮。我不希望显示该值,但是当模型回发到服务器时是必需的

tracyjuice 回答:如何在@ Html.RadioButtonFor

RadioButtonFor不会生成标记,而是生成标记。

也许可以考虑使用标签代替。

<label class="js-payment-method js-debit-card" type="button" title="Credit/Debit Card">@Html.RadioButtonFor(...) <img src="~/_common/img/payment-options/card.png"><span>Credit / Debit Card</span></label>
,

如果要将img标签嵌套在span和radio元素内。我宁愿做一个foreach并写出html。

为使单选按钮作为一个组起作用,名称属性必须相同。 ID和值可以不同。

@foreach(var x in model){
    <input type="radio" name="rad1" Value="@x.PaymentMethodSelected">
    <label>@x.PaymentMethod.Card</label>
    <img src="~/_common/img/payment-options/card.png"><!-- you can pass this uri from the model also-->
}
本文链接:https://www.f2er.com/3166139.html

大家都在问