簡體   English   中英

如何有條件地將只讀屬性設置為一行表

[英]How can I make Read Only Attribute to a row of table conditionally

我試圖使行只讀屬性的所有 TD 元素取決於 model 中列的值。 這是下面給出的詳細信息。 如果 model.IsProtected 的值為真,那么該行的所有 Td 元素都必須是只讀屬性。 應該保護它不被編輯。 請幫助建議的代碼

model class

EmployeeModel
IsProtected boolean
EmployeeName string
Salary decimal
Bonus decimal

 @Model List<EmployeeModel> for (var i = 0; i < Model.Count; i++) { <td> <input asp-for="@Model.IsProtected" type="hidden" /> </td> <td>@Model.EmployeeName</td> <td> <div > <input type="number" placeholder="0.00" style="text-align:center;vertical-align:central" class="form-control format-text" asp-for="@Model.Salary" /> </div> </td> <td> <div > <input type="number" placeholder="0.00" style="text-align:center;vertical-align:central" class="form-control format-text" asp-for="@Model.Bonus" /> </div> </td> }

謝謝

您可以使用三元條件運算符?:如下所示:

@model List<EmployeeModel>
@for (var i = 0; i < Model.Count; i++)
{
<td>
    <input asp-for="@Model[i].IsProtected" type="hidden" />
</td>
<td>@Model[i].EmployeeName</td>
<td>
    <div>
        <input type="number" placeholder="0.00" style="text-align:center;vertical-align:central" class="form-control format-text" 
               asp-for="@Model[i].Salary" readonly="@(Model[i].IsProtected ? "readonly" : null )"/>
    </div>
</td>
<td>
    <div>
        <input type="number" placeholder="0.00" style="text-align:center;vertical-align:central" class="form-control format-text" 
               asp-for="@Model[i].Bonus" readonly="@(Model[i].IsProtected ? "readonly" : null )"/>
    </div>
</td>
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM