【问题标题】:How can i set readonly property in razor?如何在剃须刀中设置只读属性?
【发布时间】:2015-04-21 09:41:36
【问题描述】:
@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>
     }

【问题讨论】:

    标签: razor


    【解决方案1】:

    EditorFor 没有过载,以 htmlAttributes 作为参数...

    你可以简单地做一个 if else

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

    【讨论】:

      【解决方案2】:

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

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

      只需阅读您对其他答案的评论

      当数量不为空且只读时,我需要将只读设置为 true 金额为空时为假

      一个简单的 if else 就足够了。

      【讨论】:

        【解决方案3】:

        试试这个:

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

        【讨论】:

        • 我需要在数量不为空时将只读设置为真,在数量为空时将只读为假
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-30
        • 1970-01-01
        • 2022-01-18
        • 2019-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多