C# 基于操作的重定向ASP.Net Core Razor

C# 基于操作的重定向ASP.Net Core Razor,c#,html,asp.net-core,razor-pages,html-input,C#,Html,Asp.net Core,Razor Pages,Html Input,我所在地的概要: 在第1页的“我的程序”中,他们选择一台打印机,然后单击一个按钮,该按钮将提交服务呼叫并加载下一页“机器信息”。重定向操作调用以下方法: public async Task<IActionResult> OnGetSelectThisPrinter(int? printerID) { var printerServiceCall = new Service_Calls { PrinterID = (int

我所在地的概要: 在第1页的“我的程序”中,他们选择一台打印机,然后单击一个按钮,该按钮将提交服务呼叫并加载下一页“机器信息”。重定向操作调用以下方法:

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");
    }
公用异步任务OnGetSelectThisPrinter(int?printerID)
{
var printerServiceCall=新服务调用
{
PrinterID=(int)PrinterID,
时间=日期时间。现在,
日期=日期时间。现在
};
_context.Service_Calls.Add(printerServiceCall);
wait_context.SaveChangesAsync();
var ServiceCalls=来自_context.Service_调用中的m
选择m;
Service_Calls ServiceCall=ServiceCalls.Last();
返回RedirectToPage(“机器信息?id=“+ServiceCall.id”);
//返回页首(“/索引”);
}
在这段代码中,printerID=1是一个占位符,直到我解决了如何传递特定机器的详细信息(任何关于传递的帮助都会很有帮助)。 此页面的HTML及其按钮如下所示:

    <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>

@Html.DisplayNameFor(model=>model.Printer[0].MachineID)
@Html.DisplayNameFor(model=>model.Printer[0].Log\u ID)
@Html.DisplayNameFor(model=>model.Printer[0]。租约ID)
@Html.DisplayNameFor(model=>model.Printer[0]。序列号)
@Html.DisplayNameFor(model=>model.Printer[0]。解决方案)
@Html.DisplayNameFor(model=>model.Printer[0]。其他信息)
@Html.DisplayNameFor(model=>model.Printer[0].InstallationDate)
@Html.DisplayNameFor(model=>model.Printer[0]。单击鼠标)
@Html.DisplayNameFor(model=>model.Printer[0]。颜色(单击)
@Html.DisplayNameFor(model=>model.Printer[0]。最小值)
@Html.DisplayNameFor(型号=>model.Printer[0]。零件\保修)
@Html.DisplayNameFor(model=>model.Printer[0]。IT\u支持)
@Html.DisplayNameFor(model=>model.Printer[0]。合同类型)
选择打印机
@foreach(Model.Printer中的变量项){
@DisplayFor(modelItem=>item.MachineID)
@DisplayFor(modeleItem=>item.Log\u ID)
@DisplayFor(modeleItem=>item.Lease\u ID)
@DisplayFor(modelItem=>item.Serial\u编号)
@DisplayFor(modelItem=>item.Solution)
@DisplayFor(modelItem=>item.Additional\u信息)
@DisplayFor(modelItem=>item.InstallationDate)
@DisplayFor(modelItem=>item.Mono\u单击)
@DisplayFor(modelItem=>item.Color\u单击)
@DisplayFor(modelItem=>item.Minimum)
@Html.DisplayFor(modelItem=>item.Parts\u保修)
@DisplayFor(modelItem=>item.IT支持)
@DisplayFor(modelItem=>item.Contract\u类型)
}

然而,目前按钮没有任何作用。看起来他们不会使用OnGetSelectThisPrinter方法。任何帮助都将不胜感激。

首先,本教程将对我即将给出的答案进行更深入的讨论。请查看:

你好!让我们从让按钮工作开始。为了将用户重定向到页面的特定操作,我们可以使用
asp页面处理程序
属性指定处理请求的方法。此外,我们使用
asp route id
属性来描述我们选择的打印机。最后,我们将所有这些都打包成一个表单。所以,我们替换

<input type="button" onclick="SelectThisPrinter()" value="Select Printer" />


然后,在代码隐藏中,我们可以对处理程序执行一些简单的更改。我们改变

公共异步任务SelectThisPrinter() { int printerID=1; ... } 到

公用异步任务OnGetSelectThisPrinter(int?printerId)
{
...
}

我希望这有帮助。如果您还有任何问题,请告诉我。

我建议您可以使用
标记访问处理程序,因为您只需要传递一个参数

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

这真的很有效,谢谢。它按预期重定向。
<a asp-page="/Printer" asp-page-handler="SelectThisPrinter" asp-route-printerID="@item.MachineID">SelectThisPrinter</a>
public IActionResult OnGetSelectThisPrinter(int printerID)
{
    ...
}