2

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; }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
SoftwareDveloper
  • 559
  • 1
  • 5
  • 18

1 Answers1

0

I would utilize asp-route-id and asp-page-handler TagHelpers to route your ID back to your PrintReceipt() method.

You could have a button like:

<button id="btnDownload" class="btn btn-info" asp-route-id="@HttpContext.Request.Query["id"]" asp-page-handler="PrintReceipt" type="submit">Submit</button>

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.

public async Task<IActionResult> PrintReceipt(string id){
    //do something 
}

You may need to add @Tran.Tran.Processing_ref_string in your if block

Below 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

Andrew Reese
  • 854
  • 1
  • 11
  • 27
  • The tag helpers are pretty nifty. I just realized something and will update my answer. In your case you would want your asp-route-id=[@Tran.Tran.Processing_ref_string]. You also probably need to add @Tran.Processing_ref_string in your if statement as well. – Andrew Reese Aug 27 '20 at 18:51
  • I am trying to pass in the Tran object which is a List element. Is it possible to do so? Or should I go the traditional route of querying the web api with the id and get data? – SoftwareDveloper Aug 27 '20 at 21:13
  • You should be able to do both. Have you tried passing in the something like PrintReceipt(Transaction transaction){ var trans = transaction.trans; }. Is your list object being populated? – Andrew Reese Aug 28 '20 at 13:44
  • 1
    From my understanding, complex objects cannot be passed(I tried it and the value was null). So, I followed your suggestion and am passing the id to the controller and in my controller, I handle data pulling. Thank you. – SoftwareDveloper Aug 28 '20 at 15:14