How to reuse an Enum on other screens containing some different properties

Asked

Viewed 43 times

0

I’m working with a screen "Financial" and another screen "Financial Report". For the Financial screen, I created an Enum with the options "Accounts Payable and Receivables".

public enum  FinanceiroTipo
{
    [Description("CONTAS A PAGAR")]
    ContasPagar = 1,
    [Description("CONTAS A RECEBER")]
    ContasReceber = 2
}

I would like to reuse the Financial Reporting Systemmodel for the Financial Reporting screen (because I don’t see the need to duplicate Enumerators), but on the Financial Reporting screen I need to have an extra enumerator, with 3 Numerators in total: "All, Accounts Payable and Accounts Receivable".

I tried to inherit, but in C#, this is not possible to be done with Numerators.

Would anyone have any good practice tips to solve my problem? Thank you!

  • 2

    Either they are the same or they are not. DRY is not to avoid repetition: https://answall.com/q/120931/101

  • But what’s wrong with having Enum with 3 values?

  • is that in the registration screen the OTHER should not appear, because it is specific to report.

  • Todos are not a type of financial transaction, so under no circumstances should it be included in Enum FinanceiroTipo if you want to use an Enum to populate the filter on the report screen create a new FinanceiroTipoFiltro with the options you want. Or because it is just a display item, you can enjoy FinanceiroTipo and add the item in the checkbox Todos and do the treatment so that when this option is selected, simply do not filter by the type of operation in your query.

  • @Leandroangelo Good suggestion!

1 answer

0


You can create a Viewmodels and inside it you can add:

public class FinanceiroTipo
{
   public int Id { get; set; }
   public string Name { get; set; }
   public bool Todos { get; set; }
}

in the controller

private void PreencherCombos()
     {

        ViewBag.FinanceiroTipo = new List<FinanceiroTipo> {
        new FinanceiroTipo {Id = 1, Name="CONTAS A PAGAR",Todos= true },
        new FinanceiroTipo {Id = 2, Name="CONTAS A RECEBER",Todos= true },   
      };

   }

in your View:

<div class="col-md-2 mb-20">
   <label asp-for="Ativo" class="control-label"></label>
@Html.DropDownListFor(model => model.Ativo, ((IEnumerable<StatusViewModel>)ViewBag.FinanceiroTipo).Select(option => new SelectListItem
{
  Text = option.Name,
  Value = option.Id.ToString(),
Selected = (Model != null) && (option.Id == Model.Ativo)
}), "Selecione", new { @class = "form-control select2" })
<span asp-validation-for="Ativo" class="text-danger"></span>
</div> 

         

Browser other questions tagged

You are not signed in. Login or sign up in order to post.