如何检查和转换 IEnumerable 值以在视图中显示?
How to check and convert a IEnumerable value to show in the view?
我在 IEnumerable 中从数据库接收数据并在视图 ASP.Net MVC 中显示为列表。我想获取一个特定的列,其中包含的值类似于 "A" 表示活动,"D" 表示停用等。因此,我想将这些字符转换为正确的字符串以显示在视图中。这该怎么做?
我感谢任何帮助。
public string GetStatusPagamento(int fileCodigo)
{
var statusPgto = _context.GetFileByFileCode(fileCodigo).Select(s =>
s.StatusPagamento).FirstOrDefault();
#region switch pagto
switch (statusPgto.ToString())
{
case "A":
return "Ativo";
case "D":
return "Ativo";
case "E":
return "Cancelado";
case "M":
return "Reembolsado";
case "R":
return "Ativo";
case "X":
return "Cancelado";
default:
return "Indefinido";
}
#endregion
}
本来应该在视图中显示字符串而不是字符。
我找到的解决方案是在我的模型中创建另一个名为 "SwitchStatusPagto" 的属性,它创建状态的切换案例,我直接在我的视图中调用这个属性。
public string SwitchStatusPagto
{
get
{
if (!string.IsNullOrEmpty(StatusPagto))
{
switch (StatusPagto)
{
case "A":
case "D":
case "R":
return "Ativo";
case "E":
case "X":
return "Cancelado";
case "M":
return "Reembolsado";
default:
return StatusPagto;
}
}
return string.Empty;
}
}
<td>@Html.DisplayFor(fr => itemRequisicao.SwitchStatusPagto)</td>
注意:如果数据为空,"return string.Empty" 是避免空引用异常所必需的。
我在 IEnumerable 中从数据库接收数据并在视图 ASP.Net MVC 中显示为列表。我想获取一个特定的列,其中包含的值类似于 "A" 表示活动,"D" 表示停用等。因此,我想将这些字符转换为正确的字符串以显示在视图中。这该怎么做? 我感谢任何帮助。
public string GetStatusPagamento(int fileCodigo)
{
var statusPgto = _context.GetFileByFileCode(fileCodigo).Select(s =>
s.StatusPagamento).FirstOrDefault();
#region switch pagto
switch (statusPgto.ToString())
{
case "A":
return "Ativo";
case "D":
return "Ativo";
case "E":
return "Cancelado";
case "M":
return "Reembolsado";
case "R":
return "Ativo";
case "X":
return "Cancelado";
default:
return "Indefinido";
}
#endregion
}
本来应该在视图中显示字符串而不是字符。
我找到的解决方案是在我的模型中创建另一个名为 "SwitchStatusPagto" 的属性,它创建状态的切换案例,我直接在我的视图中调用这个属性。
public string SwitchStatusPagto
{
get
{
if (!string.IsNullOrEmpty(StatusPagto))
{
switch (StatusPagto)
{
case "A":
case "D":
case "R":
return "Ativo";
case "E":
case "X":
return "Cancelado";
case "M":
return "Reembolsado";
default:
return StatusPagto;
}
}
return string.Empty;
}
}
<td>@Html.DisplayFor(fr => itemRequisicao.SwitchStatusPagto)</td>
注意:如果数据为空,"return string.Empty" 是避免空引用异常所必需的。