skip to Main Content

I have a simple code where an Employee has a credit card

public class EmployeeModel : PageModel
{
    private readonly EmployeeData EmployeeData;
    private readonly CardData CardData;

    public EmployeeModel(EmployeeData EmployeeData , CardData CardData)
    {
        this.EmployeeData = EmployeeData ;
        this.CardData = CardData ;
    }

    [BindProperty(SupportsGet = true)]
    public int EmployeeID { get; set; }
    
    [BindProperty(SupportsGet = true)]
    public int CardID { get; set; }
    
    public Employee Employee { get; set; }
    public Card Card { get; set; }
    
    public void OnGet()
    {
        Employee = EmployeesData.GetEmployee(EmployeeID);
        Card = CardData.GetCard(CardID);              
    }
}
@page
@model Web.Pages.EmployeeModel
@{
}
 <table class="table table-dashboard table-borderless mb-0">
              <tbody>
                 <tr class="border-bottom border-200">
                      <td>
                          <div class="flex-1">
                              <h6>ID employee</h6>
                             </div>
                        </td>
                     <td class="align-middle text-end fw-semi-bold"><span>@Html.DisplayFor(model => model.Employee.EmployeeID)</span></td>
                   </tr>
            </tbody>
</table>

 <table class="table table-dashboard table-borderless mb-0">
           <tbody>
                <tr class="border-bottom border-200">
                      <td>
                          <div class="flex-1">
                              <h6>ID Card</h6>
                            </div>
                            </td>
                              <td class="align-middle text-end fw-semi-bold"><span>@Html.DisplayFor(model => model.Card.CardID)</span></td>
                 </tr>
       </tbody>
</table>

I try to display the values that I have in EmployeeModel to view page, but doesn’t work. I try to do the next example, to put in my OnGet(int id) parameters and it work when I have a single employee, but when I try to do something more complex and add more employee the data doesn’t display itself and the table its empty:

@page
@model Web.Pages.EmployeesModel

<div class="container-fluid">
    <div class="row">
        <div>
            <h1 class="display-6 text-primary">EMPLOYEES</h1>
        </div>
        <table class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th class="text-center">ID</th>
                </tr>
            <thead>
        <tbody>
                
@foreach (var employee in Model.employeeList)
   {
      <tr>
          <td class="text-center"><a asp-page="Employee" asp-route-id="@employee.EmployeeID">@employee.EmployeeID</a></td>
         </tr>
    }
              </tbody>
           </table>
     </div>
</div>

Any idea what’s going on?

2

Answers


  1. You need to use the Employee and Card objects in razor as follows:

    <td class="align-middle text-end fw-semi-bold"><span>@Model.Employee.EmployeeID</span></td>
    
    <td class="align-middle text-end fw-semi-bold"><span>@Model.Card.CardID</span></td>
    

    @Model allows you to access the page model object directly. In your case this would be the EmployeeModel instance.

    You can refer to the below SO topic for the purposes of @Html.DisplayFor:
    What is the @Html.DisplayFor syntax for?

    As for the last code section I am not sure where is @Model.employeeList coming from. Doesn’t this result to an error?

    Login or Signup to reply.
  2. I don’t see the referenced employeeList property in your Model that you defined. You may have it in your EmployeesModel, but it’s not in the code you posted. Ideally, you’d want a property something like below in your model.

    public List<Employee> Employees { get; set; }
    

    Then, in your OnGet() method, populate that list from your data source.

    Employees = EmployeesData.GetEmployees();
    

    Then you can loop over that model property like you’re trying to:

    @foreach (var employee in Model.Employees)
    {
        ...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search