【问题标题】:ASP.Net Core Razor Page - Pass Selected Name From One Index Page To AnotherASP.Net Core Razor 页面 - 将选定名称从一个索引页面传递到另一个索引页面
【发布时间】:2021-08-17 12:19:50
【问题描述】:

我有一个使用 Razor Pages 在 ASP.Net Core 中创建的项目。这将跟踪我们的 IT 部门对 Epicor ERP 系统所做的开发项目。到目前为止,我在主页顶部有打开相应索引页面的链接。这些页面从我们的 SQL Server 表中提取数据。其中两个页面只有一个包含元素名称列表的列,我希望能够单击这些名称中的任何一个并将所选名称传递到另一个索引页面,该页面将仅显示该名称的项目。例如,这是我们在 ERP 系统中使用的 UD 表列表(见下图)...

我希望用户能够单击此列表中的任何名称,例如第一个名称“UD02”,然后将该名称传递到第二个索引页面(见下图)...

我知道您可以将 asp-route-id 传递给详细信息、编辑和删除页面,但不确定如何将名称值传递给索引样式页面的 For Each 循环。我尝试过以类似的方式传递名称...

@page
@model EpicorDevInfo.Pages.UDTables.IndexModel

@{
    ViewData["Title"] = "UD Tables";
}

<h1>UD Tables</h1>

<p>
    <a asp-page="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.UDTable[0].TableName)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.UDTable.OrderBy(r => r.TableName))
        {
            <tr>
                <td>
                    @*@Html.DisplayFor(modelItem => item.TableName)*@
                    <a asp-page="./Data/Data" asp-route-name="@item.TableName">@Html.DisplayFor(modelItem => item.TableName)</a>
                </td>
                <td>
                    <a asp-page="./Edit" asp-route-id="@item.ID">Edit</a> |
                    <a asp-page="./Details" asp-route-id="@item.ID">Details</a> |
                    <a asp-page="./Delete" asp-route-id="@item.ID">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>

我在第二个索引页面的@page 中添加了名称,但这并没有做任何事情......

@page "{name}"
@model EpicorDevInfo.Pages.UDTables.Data.IndexModel

@{
    ViewData["Title"] = "Index";
}

<h1>@Html.DisplayFor(model => model.UDTableData[0].TableName)</h1>

<p>
    <a asp-page="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.UDTableData[0].TableName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.UDTableData[0].ColumnLabel)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.UDTableData[0].DatabaseColumn)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.UDTableData)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.TableName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ColumnLabel)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.DatabaseColumn)
                </td>
                <td>
                    <a asp-page="./Edit" asp-route-id="@item.ID">Edit</a> |
                    <a asp-page="./Details" asp-route-id="@item.ID">Details</a> |
                    <a asp-page="./Delete" asp-route-id="@item.ID">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>

这是第二个索引页面的 C# 部分...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using EpicorDevInfo.Data;
using EpicorDevInfo.Models;

namespace EpicorDevInfo.Pages.UDTables.Data
{
    public class DataModel : PageModel
    {
        private readonly EpicorDevInfo.Data.EpicorDevInfoContext _context;

        public DataModel(EpicorDevInfo.Data.EpicorDevInfoContext context)
        {
            _context = context;
        }

        public IList<UDTableData> UDTableData { get; set; }

        public async Task<IActionResult> OnGetAsync(string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                return NotFound();
            }

            UDTableData = await _context.Epi_UDTableData_TEMP.ToListAsync();

            if (UDTableData == null)
            {
                return NotFound();
            }
            return Page();
        }
    }
}

我确信这很简单,但我找不到任何可以按我想要的方式工作的东西。任何帮助将不胜感激。感谢您的宝贵时间,祝您有美好的一天!

【问题讨论】:

    标签: asp.net-mvc asp.net-core razor-pages


    【解决方案1】:

    首先,如果我使用你的代码,我可以在DataModel的OnGetAsync中获取名称。

    然后你可以尝试改变

    UDTableData = await _context.Epi_UDTableData_TEMP.ToListAsync();
    

    UDTableData = await _context.Epi_UDTableData_TEMP.Where(t=>t.TableName==name).ToListAsync();
    

    OnGetAsync(string name)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-19
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2021-01-14
      • 2020-11-20
      相关资源
      最近更新 更多