Why is model data passed from view to controller null?

833 views Asked by At

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; }
}
1

There are 1 answers

4
Andrew Reese On BEST ANSWER

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