I have a view where I loop through the model list and display data. I am trying to pass that model to a different controller/action on link click. The data being passed is null. How do I do this?
View:
@Model TransactionViewModel
<table class="table table-striped table-hover visible-lg visible-md visible-sm " style="white-space:nowrap;">
<thead>
<tr>
<th>Date</th>
<th>Amount</th>
<th>Tag Number</th>
<th>Payment Method</th>
<th>Prior Balance</th>
<th>Current Balance</th>
<th>Description</th>
<th>Comments</th>
<th>Receipt</th>
</tr>
</thead>
<tbody>
@if (Model != null && Model.Transactions != null)
{
@foreach (var Tran in @Model.Transactions)
{
<tr>
<td>@Tran.TimeStamp</td>
<td>@Tran.Fare</td>
<td>@Tran.FullTagNum</td>
<td>@Tran.PaymentMethod</td>
<td>@Tran.PreviousBalance</td>
<td>@Tran.NewBalance</td>
<td>@Tran.PaymentDescription</td>
<td>@Tran.Comments</td>
@if (Tran.Processing_ref_string != null)
{
<td>
<a href="@Url.Action("PrintReceipt", "Payment", new { ReceiptData = Tran })" target="_blank">Receipt</a>
</td> /*how do I pass in the dynamic variable Tran*/
}
else
{
<td>Not Available</td>
}
</tr>
}
}
</tbody>
</table>
Controller Action:
public async Task<IActionResult> PrintReceipt(ReplenishmentRecordResponse ReceiptData){
//data manipulation
}
Model:
public class TransactionViewModel
{
[Display(Name = "From", Prompt = "Starting Date")]
public DateTime StartDate { get; set; }
[Display(Name = "To", Prompt = "Ending Date")]
public DateTime EndDate { get; set; }
public List<ReplenishmentRecordResponse> Transactions { get; set; }
}
I would utilize
asp-route-id
andasp-page-handler
TagHelpers to route your ID back to your PrintReceipt() method.You could have a button like:
Your element needs to have
name=id
for this to work, but you can change id to be anything.Simply pass in string id as your parameter in your Controller method.
You may need to add
@Tran.Tran.Processing_ref_string
in your if blockBelow are some good links that could also steer you in the right direction. I hope this helps!
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-3.1
TagHelper for passing route values as part of a link
https://www.learnrazorpages.com/razor-pages/tag-helpers/anchor-tag-helper