如何在剃须刀中设置只读属性


@if (Model.Amount!=null)
    {
        <tr>
           <td>Amount</td>
           <td><b>:</b></td>
           <td>@Html.EditorFor(model => model.Amount, new { readonly= true})
                @Html.ValidationMessageFor(model => model.Amount)
           </td>
        </tr>
     }

EditorFor因为没有重载,需要htmlAttributes作为参数...

你可以简单地做一个如果否则

@if (Model.Amount != null) {
    @Html.TextBoxFor(model => model.Amount)
}
else {
   //or @Html.DisplayFor(model => model.Amount)
   @Html.TextBoxFor(model => model.Amount, new{@readonly = "readonly"}
}

既然你想让它只读,为什么不使用标签呢?

@Html.DisplayFor(model => model.Amount)

只需阅读您对另一个答案的评论

当金额不为空且只读时,我需要将只读设置为 true 当金额为空时为 false,则为 false

一个简单的如果其他应该就足够了。

试试这个:

@Html.EditorFor(model => model.Amount, new {  @readonly = "readonly"})

最新更新