Search code examples
c#asp.net-core.net-coreparameter-passingrazor-pages

Pass Selected Item From One Index Page To Make Title of Another Index Page


Good morning mega-minds! I have a project that I'm creating in ASP.Net Core with Razor Pages that will track development projects that our IT department has made to our Epicor ERP system. I have one Index page that lists Table Names and another that lists data related to a selected name from the first Index page. What I would like to do is have the page title of the second Index page be the selected name from the first Index page. See images below to show what I'm trying to accomplish...

UD Tables

UD Table Data

Here's the code for the 1st Index page...

@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>
                    <a asp-page="./Data/Index" asp-route-tblID="@item.ID" asp-route-tblName="@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 here's the code for the 2nd...

@page
@model EpicorDevInfo.Pages.UDTables.Data.IndexModel

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

<h1>This need to change to show the previous Index page item that was clicked on!</h1>

<p>
    <a asp-page="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.UDTableData[0].TableID)
            </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.TableID)
                </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>

Basically what I'm looking to do is pass this from the 1st page...

<td>
     <a asp-page="./Data/Index" asp-route-tblID="@item.ID" asp-route-tblName="@item.TableName">
         @Html.DisplayFor(modelItem => item.TableName)
     </a>
</td>

To this on the 2nd page...

<h1>This need to change to show the previous Index page item that was clicked on!</h1>

I've looked up how to use ViewData and ViewBag, but nothing showed how to assign what a user selects to either of those. Any help would be greatly appreciated. Thank you for your time and have a great day!


Solution

  • you have to change view and code behind for the second page

    view

    @page"{id}/{tblname}"
    @model EpicorDevInfo.Pages.UDTables.Data.IndexModel
    

    and code behind

    public void OnGet(int id, string tblname)