1

I have a View with a table

Here is the code of the View:

 @foreach (var item in Model)
{
    <tr>
        <td class="point">
            @(rowNo += 1)
        </td>
        <td class="FIO" style="text-align: center; font-size: 16px; cursor:pointer">@Html.DisplayFor(modelItem => item.FIO) @Html.Hidden("clientEmail", item.FIO)</td>

        <td style="text-align: center; font-size: 16px;">
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td style="text-align: center; font-size: 16px;">
            @Html.DisplayFor(modelItem => item.City)
        </td>
        <td style="text-align: center; font-size: 16px;">
            @Html.DisplayFor(modelItem => item.Birthday)
        </td>
        <td style="text-align: center; font-size: 16px;">
            @Html.DisplayFor(modelItem => item.Salary)
        </td>
        <td style="text-align: center; font-size: 16px;">
            @Html.DisplayFor(modelItem => item.English)
        </td>
      <td style="text-align: end;">
            <a href='@Url.Action("Edit", "Interwier", new {id = item.Interwier_id})'>
                <img src='@Url.Content("~/Images/Edit.png")' />
            </a>
            <a href='@Url.Action("Delete", "Interwier", new {id = item.Interwier_id})'>
                <img src='@Url.Content("~/Images/Delete.png")' />
            </a>
        </td>
    </tr>
}

This code is about Age:

<td style="text-align: center; font-size: 16px;">
    @Html.DisplayFor(modelItem => item.Birthday)
</td>

It returns me year from database. I need to show age.

How I can do this correctly?

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
S.E
  • 49
  • 1
  • 10

1 Answers1

2

By your description, your Birthday is a year value you can:

first add a new property in your model used in view for generating Age:

public class ModelName
{
    // other properties
    // original Birthday property
    public int Birthday {get;set;}

    // get age by subtracting current year with Birthday year.
    public int Aget {get {return DateTime.Now.Year - Birthday;}}
}

then in your view

change displaying Birthday to Age

 <td style="text-align: center; font-size: 16px;">
            @Html.DisplayFor(modelItem => item.Age)
 </td>
Alan Tsai
  • 2,465
  • 1
  • 13
  • 16