Rookie Programmer - Razor Pages Asp Net Core, Scaffolding Display Name

Chris Shoemaker 1 Reputation point
2022-02-22T12:50:10.387+00:00

Good Day! Rookie Programmer here.

Tables

MaintenanceRequest (table)

MaintenanceRequestNbr
Name
Desc
CreateDate
CreatePersNbr

Person (table)

PersNbr
Name
Birthdate

When scaffolding the CRUD pages for the MaintenanceRequest Entity, the list\index page results in showing the person Name which is great.
Most times it shows the navigation primary key which in this case is the PersNbr. All my other scaffolding for CRUD pages uses the primary key.
Obviously this doesn't read well, we want to see the persons name and most times the description or name NOT the key itself (PersNbr)
I can't figure out why scaffolding this table gives me the results I want, because I want to mimic this behavior for all other navigation tables.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
741 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,553 questions
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 28,131 Reputation points
    2022-02-22T22:58:40.59+00:00

    You only showed a snippet of code. According to the snippet is looks like a list.

    As explained above the Id is located in a link which identifies the list item. When the link is clicked then you're either opening the edit or details view.

    See the following example. At the end of each row you'll find Edit, Details, and Delete links. Each has a route id which is the record id; @item.CourseID.

    <h1>Courses</h1>
    
    <p>
        <a asp-page="Create">Create New</a>
    </p>
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Courses[0].CourseID)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Courses[0].Title)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Courses[0].Credits)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Courses[0].Department)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
    @foreach (var item in Model.Courses)
    {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.CourseID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Title)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Credits)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Department.Name)
                </td>
                <td>
                    <a asp-page="./Edit" asp-route-id="@item.CourseID">Edit</a> |
                    <a asp-page="./Details" asp-route-id="@item.CourseID">Details</a> |
                    <a asp-page="./Delete" asp-route-id="@item.CourseID">Delete</a>
                </td>
            </tr>
    }
        </tbody>
    </table>
    

  2. AgaveJoe 28,131 Reputation points
    2022-02-25T12:56:53.647+00:00

    How can we scaffold these CRUD pages to use item.Department.Name instead of item.Department.Nbr.

    If I understand correctly, you do not want the "Nbr" named properties to show up as inputs or in a list. If you cannot use the "Id" naming convention then set the key using an attribute or use the Fluent API.

    https://learn.microsoft.com/en-us/ef/core/modeling/keys?tabs=fluent-api

            public DbSet<Department2> Departments2 { get; set; }  
            protected override void OnModelCreating(ModelBuilder modelBuilder)  
            {  
                modelBuilder.Entity<Department2>().HasKey(m => m.DepartmentNbr);  
            }  
    

    Scaffolding produces the following.

    @page  
    @model ContosoUniversity.Pages.Test.IndexModel  
      
    @{  
        ViewData["Title"] = "Index";  
    }  
      
    <h1>Index</h1>  
      
    <p>  
        <a asp-page="Create">Create New</a>  
    </p>  
    <table class="table">  
        <thead>  
            <tr>  
                <th>  
                    @Html.DisplayNameFor(model => model.Department2[0].Name)  
                </th>  
                <th></th>  
            </tr>  
        </thead>  
        <tbody>  
    @foreach (var item in Model.Department2) {  
            <tr>  
                <td>  
                    @Html.DisplayFor(modelItem => item.Name)  
                </td>  
                <td>  
                    <a asp-page="./Edit" asp-route-id="@item.DepartmentNbr">Edit</a> |  
                    <a asp-page="./Details" asp-route-id="@item.DepartmentNbr">Details</a> |  
                    <a asp-page="./Delete" asp-route-id="@item.DepartmentNbr">Delete</a>  
                </td>  
            </tr>  
    }  
        </tbody>  
    </table>  
    

    I think it is worth mentioning that a large project with 262 entities and many relationships is probably not a good candidate for Razor Pages scaffolding anyway. Usually a few more application layer are needed.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.