Home > database >  Retrieve index of for loop that populates a table View to use in model C# Razor pages
Retrieve index of for loop that populates a table View to use in model C# Razor pages

Time:08-14

I have a challenge I am struggling to overcome. I have the following html :

         @for (int i = 0; i < Model.OrderBoxes.Count; i  ) {
            Model.Boxid = Model.OrderBoxes[i].Box.Id;
                
                <tr>
                <td data-label="Ref">@Model.OrderBoxes[i].Box.Id.ToString().Split("-")[0]</td>
                <td data-label="Description">@Model.OrderBoxes[i].Box.Description</td>
                <td data-label="Length">@Model.OrderBoxes[i].Box.InsideLength</td>
                <td data-label="Width">@Model.OrderBoxes[i].Box.InsideWidth</td>
                <td data-label="Height">@Model.OrderBoxes[i].Box.InsideHeight</td>
                <td data-label="HssLid">@Model.OrderBoxes[i].Box.isLid</td>
                <td data-label="HasFoot">@Model.OrderBoxes[i].Box.isFoot</td>
                <td data-label="Quantity">@Model.OrderBoxes[i].Box.Quantity</td>
                <td data-label="Quantity Complete">@Model.OrderBoxes[i].Box.QuantityComplete</td>
                <td data-label="Expected Date">@Model.OrderBoxes[i].Box.ExpectedDate.ToString("dd/MM/yyyy")</td>
                <td data-label="Edit"><div>Box Id @Model.Boxid </div>    <a asp-route-id="@Model.Order.Id" asp-page-handler="Edit">Edit</a>
                </td>
                <td data-label="EnterComplete"><form    asp-page-

And a Controller that looks like this :

   public async Task<IActionResult> OnGetEditAsync(int? id)
    {
    if (id == null)
      {
        return NotFound();
      }
     OrderBoxes= await _context.OrderBoxes
     .Include(q => q.Box)
     .Where(q => q.Order.Id == Order.Id)
     .ToListAsync();
     OrderPallets = await _context.OrderPallets
     .Include(q => q.Pallet)
     .Where(q => q.Order.Id == Order.Id).ToListAsync();
     Boxes = await _context.Boxes.ToListAsync();

   Box b = await _context.Boxes.FirstOrDefaultAsync(m => m.Id == Boxid);


        Bo=b;
    
  
        return Page();
    }

I have bound the following :

public List<OrderBox> OrderBoxes { get; set; } = new List<OrderBox>();
public List<OrderPallet> OrderPallets { get; set; } = new List<OrderPallet>();
public List<Box> Boxes { get; set; } = new List<Box>();
[BindProperty(SupportsGet = true)]
public Order Order { get; set; } = new Order();
[BindProperty(SupportsGet = true)]
public int? Boxid { get; set; }
[BindProperty]
public Box Bo { get; set; }

But facing a problem. To get the correct box id, I need to know what index of the of loop the edit button sits ... I.e. :

Box b = await _context.Boxes.FirstOrDefaultAsync(m => m.Id == Boxid);

I however cannot get Boxid to return

Model.Boxid = Model.OrderBoxes[i].Box.Id;

although Model.Boxid is bound 2 way.

Can anyone help with suggesting how I can go about it?

I get the error : Boxid is null, although in the view it displays the correct value.

CodePudding user response:

You have to send the parameter to the server from the anchor tag helper:

<a asp-route-BoxId="@Model.OrderBoxes[i].Box.Id" asp-route-id="@Model.Order.Id" asp-page-handler="Edit">Edit</a>
  • Related