剃刀页面:在导航时将多个参数传递给OnGetAsync

时间:2018-10-19 22:27:49

标签: c# asp.net razor-pages

我正在研究表示模型import os, os.path import win32com.client if os.path.exists('C:/Users/jz/Desktop/test.xlsm'): excel_macro = win32com.client.DispatchEx("Excel.Application") # DispatchEx is required in the newest versions of Python. excel_path = os.path.expanduser('C:/Users/jz/Desktop/test.xlsm') workbook = excel_macro.Workbooks.Open(Filename = excel_path, ReadOnly =1) excel_macro.Application.Run("test.xlsm!Module1.Macro1") # update Module1 with your module, Macro1 with your macro workbook.Save() excel_macro.Application.Quit() del excel_macro Lists的Razor Page项目:

Records

记录列表的子级。 DB中的一对多: enter image description here

我从public class List { public int ID { get; set; } [StringLength(25, MinimumLength = 3, ErrorMessage = "Title should be in range between 3 and 25")] [Display(Name = "Title")] public string Caption { get; set; } public string UserId { get; set; } public User User { get; set; } public ICollection<Record> Records { get; set; } } public class Record { public int ID { get; set; } [StringLength(25, MinimumLength = 3, ErrorMessage = "Word should be in range between 3 and 25")] [Display(Name = "Title")] public string Word { get; set; } [StringLength(30, MinimumLength = 3, ErrorMessage = "Translation should be in range between 3 and 30")] public string Translation { get; set; } public int ListId { get; set; } public List List { get; set; } } 打开Records的列表时建立了有效的URL: enter image description here

现在,我想为单个Lists构建类似的URL,如下所示: enter image description here 但是,为此,我需要通过Record文件正确传递多个参数:ListIdRecordId。像这样:

csgtml

“记录/详细信息”(我要浏览的页面)中方法 <a asp-page="./Details" asp-route-id="@item.ID,@item.ListId">Details</a> 的签名应如下所示:

OnGetAsync

public async Task<IActionResult> OnGetAsync(int? id, int? listId) {...} 中页面的定义应类似于此:

Records/Details.cshtml

但这不起作用!

如果我这样定义@page "/Lists/{listId}/Records/{id}/Details/"

@page Records/Details.cshtml

方法

@page "/Lists/{id}/Records/Details/"

转到断点,但是,两个参数均为null,URL如下所示: enter image description here

2 个答案:

答案 0 :(得分:3)

您几乎可以使用它,但是您需要拆分这些路由参数并使用类似以下内容的方法:

<a asp-page="./Details" asp-route-id="@item.ID" asp-route-listId="@item.ListId">Details</a>

您使用asp-route-*来设置参数,其中*表示参数本身的名称,并用于每个单独的参数。

答案 1 :(得分:2)

如果您需要指定多个路由值,则可以使用锚标记帮助器上的asp-all-route-data属性:

@{   
    var routeValues = new Dictionary<string, string>
        {
           { "listId", item.ListId },
           { "id", item.ID }
        };
}

<a asp-page="./Details" asp-all-route-data="routeValues">Click</a>