asp.net-corepostrazor-pagesmaster-detail

How to post table cell value in a Razor page


Looking for ideas on how to pass the 'RecordId' value for the specific row in the table, when the 'view details' button for that row is pushed. This project is .NET Core 5 Web app with Razor pages. I'm not set on the button, open to other ideas to post the value and display the details. Here is snippet of code generating the table rows:

                    @foreach(var x in  Model.masterrecords)
                    {
                        <tr class="border text-center">
                            <td>@x.RecordId</td>
                            <td>@x.FirstName</td>
                            <td>@x.LastName</td>
                            <td>@x.NumberOfRecords</td>
                            <td>
                                <input type="submit" asp-page-handler="ViewDetailsBtn" value="view details" class="btn btn-sm" />
                            </td>
                        </tr>
                    }

Solution

  • Try to use a form to post RecordId:

    @foreach (var x in Model.masterrecords)
            {
                <tr class="border text-center">
                    <td>@x.RecordId</td>
                    <td>@x.FirstName</td>
                    <td>@x.LastName</td>
                    <td>@x.NumberOfRecords</td>
                    <td>
                        <form method="post"  asp-page-handler="ViewDetailsBtn" asp-route-RecordId=@x.RecordId>
                            <input type="submit" value="view details" class="btn btn-sm" />
                        </form>
                    </td>
                </tr>
            }
    

    handler(If your RecordId is string type,try to use string RecordId):

    public void OnPostViewDetailsBtn(int RecordId)
            {
                
            }
    

    result: enter image description here