I have a project that I'm creating in ASP.Net Core with Razor Pages. This will track development projects that our IT department has made to our Epicor ERP system. So far, I have links at the top of the main page that opens the corresponding Index pages. These pages pull in data from our SQL Server tables. Two of these pages only have a single column with a list of element names, and I would like to be able to click on any of these names and pass the selected name to another Index page that will show only items for that name. For example, this is a list of UD Tables we've used in our ERP system (see image below)...
I would like for a user to be able to click on any name in this list, let's say the 1st name "UD02", and pass that name to the second Index page (see image below)...
I know you can pass asp-route-id to Details, Edit, and Delete pages, but not sure how to pass a name value to the For Each loop of an Index style page. I have tried passing the name in a similar way...
@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>
And I added name to the @page of the second Index page, but that didn't do anything...
@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>
And here is the C# part of the second Index page...
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();
}
}
}
I'm sure it's something simple, but I haven't been able to find anything that works the way I'm wanting. Any help would be greatly appreciated. Thank you for your time and have a great day!
Firstly,if I use the code of you,I can get the name in OnGetAsync
of DataModel.
And then you can try to change
UDTableData = await _context.Epi_UDTableData_TEMP.ToListAsync();
to
UDTableData = await _context.Epi_UDTableData_TEMP.Where(t=>t.TableName==name).ToListAsync();
in OnGetAsync(string name)