How to display name instead of id in ASP.NET Core?
NickName:Trần Quang Khải Ask DateTime:2022-12-23T10:33:55

How to display name instead of id in ASP.NET Core?

Im using visual studio with ASP.NET Core web App (.NET 6.0).Im using Microsoft SQL Server Management Studio 19 as database. Currently, i ran a problem where it display the id instead of name in book index.

I have category table in the database and show it in drop box

enter image description here

But when I show product detail in the index, the category become ID of the category

enter image description here

here is my code: https://github.com/thorgia1702/FPTBook

I have tried make the category name become the FK key in the book table in the Migrations file but have not success

Copyright Notice:Content Author:「Trần Quang Khải」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74895615/how-to-display-name-instead-of-id-in-asp-net-core

Answers
Qing Guo 2022-12-23T06:35:22

I test your project, and I have the idea like below, could you try the below code?\n\nRemove this line from your .csproj file, read this to know more:\n\n<Nullable>enable</Nullable>\nIn BookController:\n1.change the index action to add .Include to use the Include method to specify related data to be included in query results. :\n public async Task<IActionResult> Index()\n {\n return View(await _context.Books.Include(x=>x.Category).ToListAsync());\n }\n\n2.Change the post create action like your create action in CategoryController:\n public async Task<IActionResult> Create([Bind("Id,Title,ReleaseDate,Price,CategoryId")] Book model)\n {\n if (ModelState.IsValid)\n {\n _context.Add(model);\n await _context.SaveChangesAsync();\n return RedirectToAction(nameof(Index));\n }\n ViewData["CategoryId"] = new SelectList(_context.Set<Category>(), "CategoryId", "Name",model.CategoryId);\n return View(model);\n }\n\n3.result:\n",


More about “How to display name instead of id in ASP.NET Core?” related questions

How to display name instead of id in ASP.NET Core?

im using visual studio with asp.net core.My database is MSSQL. Im following tutorial from youtube. Currently i ran a problem where it display the id instead of name. Here is the picture of what i t...

Show Detail

How to display name instead of id in ASP.NET Core?

Im using visual studio with ASP.NET Core web App (.NET 6.0).Im using Microsoft SQL Server Management Studio 19 as database. Currently, i ran a problem where it display the id instead of name in book

Show Detail

How to obtain the ASP.NET Core application name?

I would like do display in the footer of an ASP.NET Core 1.1 &copy; 2017 MyApplication &lt;p&gt;&amp;copy; 2017 @(ApplicationName)&lt;/p&gt; How do I get the application name? I found an

Show Detail

How to display plain text instead of with HTMLText in textarea in ASP.Net Core

How to display plain text instead of with HTMLText in textarea in ASP.Net Core this is my view &lt;div class=&quot;form-group row&quot;&gt; &lt;label asp-for=&quot;Decription&quot; ...

Show Detail

ASP.NET Core ViewLocalizer display key instead of the value

I'm working on implement localization in my ASP.NET Core application. My application is multilingual and I want to support two languages (English and Arabic), I followed and configure everything as...

Show Detail

How to display name instead of the id?

I have problem, I don't know how to display the name of the department or program instead of their id. I am currently editing the user information but when I look to the department, the one who dis...

Show Detail

Show enum name value for corresponding Id in .NET Core

I am developing a web application and .NET Core and recently bumped against this issue. It shows the Id of the enum value in: @Html.DisplayFor(model =&gt; model.MutatieReden) but I want to show th...

Show Detail

Asp.net mvc4 and Entity framework, display Name instead of displaying Id for two related tables

I have two tables, Book and Category. ------------- -------------- | book | | Category | ------------- -------------- |Id | ...

Show Detail

How to display name instead of id

I have two tables: albums and categories. I am displaying albums details with a foreach loop that includes category_id in my index page. I want to display category_name instead of category id....

Show Detail

SQL - Display name instead of ID

I have two tables DragRace and Team: dragrace id | date | team_one_id | team_two_id | tournament_id team id | name What I am trying to do is display the names instead of ID's with the date...

Show Detail