我有一個這樣的頁面:

如您所見,Urgent 和 Done 部分中有 True 和 False 值。我想更改這些帖子。
我希望它寫“是”如果True和“否”如果False。我怎么能這樣做?
我的代碼:
@using ToDoListApp.Models
@model List<TodoTable>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Task</th>
<th scope="col">Urgent</th>
<th scope="col">Done</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<th scope="row">@item.Id</th>
<td>@item.Name</td>
<td>@item.Urgent</td>
<td>@item.Done</td>
</tr>
}
</tbody>
</table>
uj5u.com熱心網友回復:
您可以使用@ifor 來獲得更簡潔的代碼,您可以使用三元運算子: ?。
<td>@(item.Urgent ? "Yes" : "No")</td>
<td>@(item.Done ? "Yes" : "No")</td>
uj5u.com熱心網友回復:
@Fadi Hania的回答是正確的,但我想提出一種不同的方法。您可以在模型類中添加兩個計算屬性。這樣,如果您需要TodoTable在不同的視圖中顯示,您不必重寫邏輯。
public class TodoTable
{
public int Id { get; set; }
public string Name { get; set; }
public bool Urgent { get; set; }
public bool Done { get; set; }
public string UrgentDisplayValue => Urgent ? "Yes" : "No";
public string DoneDisplayValue => Done ? "Yes" : "No";
}
用法:
@foreach (var item in Model)
{
<tr>
<th scope="row">@item.Id</th>
<td>@item.Name</td>
<td>@item.UrgentDisplayValue</td>
<td>@item.DoneDisplayValue</td>
</tr>
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/508224.html
標籤:asp.net-mvc asp.net 核心
上一篇:foreach..型別為“TodoTable”的變數,因為“TodoTable”不包含“GetEnumerator”錯誤的公共實體或擴展定義
下一篇:為什么授權身份無法正常作業?
