2

Enum:

    public enum Enumenter
    {
        Products = 10,
        Users = 20,
        Actions = 30
    }

my view:

@model IEnumerable<ServerDB.Tables.EntityType>

@{

    ViewBag.Title = "Infrastruktur";
}


<table class="table">
<tr>
    <th>

        <h2>Infrastruktur</h2>
    </th>

    <th></th>
</tr>

@foreach (var val in Enum.GetNames(typeof(ServerDB.Tables.Enum)))
{


    <tr>
        <td>
            @Html.DisplayName((String.Format("{0}{1}", val, "2")))

        </td>
        <td></td>
        <td>
            <p>
            </p>
        </td>
        <td></td>
    </tr>
}
@foreach (var Ost in Enum.(typeof(ServerDB.Tables.Enum)))
{
    @Html.DisplayName((String.Format("{0}", Ost)))
}

</table>

How do i Print out The values next to the products? im going to use the numbers from the enum to make a query search, so i can search for any product, any users or actions for that matter. im no expert in this, so please be gentle.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
user3439890
  • 21
  • 1
  • 2

1 Answers1

3

Enum.GetValues returns values for the enum as objects, so you need to cast them to int value (using int instead of var will do that for you). After that you can use Enum.GetName to get the name for specific value:

@foreach (int val in Enum.Values(typeof(ServerDB.Tables.Enum)))
{
   var name = Enum.GetName(typeof(ServerDB.Tables.Enum), val);

    <tr>
        <td>
            @Html.DisplayName((String.Format("{0}{1}", name, "2")))

        </td>
        <td>
            @val
        </td>
        <td>
            <p>

            </p>
        </td>
        <td>

        </td>
    </tr>
}

There is another approach which doesn't use the Enum.GetName method. You simply cast the value you get from Enum.Values to string to get the name and to int to get the value

@foreach (var name in Enum.Values(typeof(ServerDB.Tables.Enum)))
{
    var val = (int)name;
    <tr>
        <td>
            @Html.DisplayName((String.Format("{0}{1}", name, "2")))

        </td>
        <td>
            @val
        </td>
        <td>
            <p>

            </p>
        </td>
        <td>

        </td>
    </tr>
}
Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50