Search code examples
c#htmlasp.net-corerazor-pageshtml-input

Action based redirect ASP.Net Core Razor


Synopsis of the place I am at: In my program on page 1 they select a printer and click a button which submits a service call and loads the next page, machine information. The redirect action calls the following method:

public async Task<IActionResult> OnGetSelectThisPrinter(int? printerID)
    {
        var printerServiceCall = new Service_Calls
        {
            PrinterID = (int)printerID,
            Time = DateTime.Now,
            Date = DateTime.Now               
        };
        _context.Service_Calls.Add(printerServiceCall);
        await _context.SaveChangesAsync();
        var ServiceCalls = from m in _context.Service_Calls
                           select m;
        Service_Calls ServiceCall = ServiceCalls.Last();            
        return RedirectToPage("MachineInformaion?id=" + ServiceCall.ID);
        //return RedirectToPage("./Index");
    }

In this code the printerID = 1 is a placeholder until I work out how to pass in a specific machines details (any help on passing through that would be helpful). The HTML for this page, with the buttons, is as follows:

    <table class="table">
    <thead>
        <tr>
            <th>

                @Html.DisplayNameFor(model => model.Printer[0].MachineID)
            </th>
            <th>
                <a asp-page="./SelectPrinter" asp-route-sortOrder="@Model.LogSortParam">@Html.DisplayNameFor(model => model.Printer[0].Log_ID)</a>
            </th>
            <th>
                <a asp-page="./SelectPrinter" asp-route-sortOrder="@Model.LeaseSortParam">@Html.DisplayNameFor(model => model.Printer[0].Lease_ID)</a>
            </th>
            <th>
                <a asp-page="./SelectPrinter" asp-route-sortOrder="@Model.SerialSortParam">@Html.DisplayNameFor(model => model.Printer[0].Serial_Number)</a>
            </th>
            <th>
                <a asp-page="./SelectPrinter" asp-route-sortOrder="@Model.SolutionSortParam">@Html.DisplayNameFor(model => model.Printer[0].Solution)</a>
            </th>
            <th>
                <a asp-page="./SelectPrinter" asp-route-sortOrder="@Model.AdditionalSortParam">@Html.DisplayNameFor(model => model.Printer[0].Additional_Info)</a>
            </th>
            <th>
                <a asp-page="./SelectPrinter" asp-route-sortOrder="@Model.InstallationSortParam">@Html.DisplayNameFor(model => model.Printer[0].InstallationDate)</a>
            </th>
            <th>
                <a asp-page="./SelectPrinter" asp-route-sortOrder="@Model.MonoClickSortParam">@Html.DisplayNameFor(model => model.Printer[0].Mono_Click)</a>
            </th>
            <th>
                <a asp-page="./SelectPrinter" asp-route-sortOrder="@Model.ColourCLickSortParam">@Html.DisplayNameFor(model => model.Printer[0].Colour_Click)</a>
            </th>
            <th>
                <a asp-page="./SelectPrinter" asp-route-sortOrder="@Model.MinimumSortParam">@Html.DisplayNameFor(model => model.Printer[0].Minimum)</a>
            </th>
            <th>
                <a asp-page="./SelectPrinter" asp-route-sortOrder="@Model.PartsSortParam">@Html.DisplayNameFor(model => model.Printer[0].Parts_Warranty)</a>
            </th>
            <th>
                <a asp-page="./SelectPrinter" asp-route-sortOrder="@Model.ITSortParam">@Html.DisplayNameFor(model => model.Printer[0].IT_Support)</a>
            </th>
            <th>
                <a asp-page="./SelectPrinter" asp-route-sortOrder="@Model.ContractSortParam">@Html.DisplayNameFor(model => model.Printer[0].Contract_Type)</a>
            </th>
            <th>Select Printer</th>
        </tr>
    </thead>
    <tbody>

@foreach (var item in Model.Printer) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.MachineID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Log_ID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Lease_ID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Serial_Number)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Solution)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Additional_Info)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.InstallationDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Mono_Click)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Colour_Click)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Minimum)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Parts_Warranty)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IT_Support)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Contract_Type)
            </td>
            <td>
                <form method="get" asp-page-handler="OnGetSelectThisPrinter" asp-route-id="@item.MachineID">
                    <button value="Select Printer" />
                </form>
            </td>
        </tr>
}
    </tbody>
</table>

However, at the moment the buttons don't do anything. It looks like they aren't going into the OnGetSelectThisPrinter Method. Any help would be greatly appreciated.


Solution

  • I suggest you can use <a> tag to access the handler, since you only need to pass a parameter.

    <a asp-page="/Printer" asp-page-handler="SelectThisPrinter" asp-route-printerID="@item.MachineID">SelectThisPrinter</a>
    

    Handler in Printer page:

    public IActionResult OnGetSelectThisPrinter(int printerID)
    {
        ...
    }