【问题标题】:How to check and convert a IEnumerable value to show in the view?如何检查和转换 IEnumerable 值以显示在视图中?
【发布时间】:2019-09-07 00:21:57
【问题描述】:

我在 IEnumerable 中从 DB 接收数据并在 View 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
}

假设在视图中显示字符串而不是字符。

【问题讨论】:

    标签: c# asp.net-mvc list ienumerable


    【解决方案1】:

    我找到的解决方案是在我的模型中创建另一个名为“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”以避免空引用异常。

    【讨论】:

      猜你喜欢
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 2011-07-15
      • 2011-06-12
      • 2017-01-25
      • 1970-01-01
      相关资源
      最近更新 更多