How to display name instead of id in ASP.NET Core?
NickName:Mana Ask DateTime:2022-03-25T15:36:21

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 try to create/add enter image description here Here is the picture of my problem enter image description hereSince i dont know what code to include i just give my github link [text]

Copyright Notice:Content Author:「Mana」,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/71613506/how-to-display-name-instead-of-id-in-asp-net-core

Answers
Xinran Shen 2022-03-28T03:22:52

the type of model.COURT_TYPE is int, so the page display 1 is no problem, But your prupose is display the dropdownlist's text not the value, So you can use linq to select in viewbag.\nI write a simple demo here.\n //Here the Id is your COUNT_TYPE, in your code, \n you use select from db to get the COUNT_TYPE, \n For testing convenience, I just hard code here to get the \n data from other action.\n\n //In your code, You save the value and text of the dropdownlist in the database, \n I just hard code here and use `items` of type List<T> to include them.\n\npublic IActionResult Privacy(int id)\n {\n\n List<SelectListItem> test = new List<SelectListItem>();\n\n foreach (var item1 in items)\n {\n test.Add(new SelectListItem { Text = item1.Name, Value = item1.Id.ToString() });\n }\n ViewBag.Items1 = test;\n\n\n Item item = new Item();\n item.Id = id;\n return View(item);\n }\n\npage\n@model ViewBagTest.Models.Item\n@{\n ViewData["Title"] = "Privacy Policy";\n var test = ((List<SelectListItem>)ViewBag.Items1).Where(x => x.Value == Model.Id.ToString()).Select(x=>x.Text).First();\n}\n<h1>@ViewData["Title"]</h1>\n<h1>@test</h1>\n\n",


Manektech Knowledge Base 2022-03-25T09:27:50

As you said it is displaying ID instead of name because in your code, you are binding ID itself.\nIn your index view,\n <th>\n <i class="@sortModel.GetColumn("COURT_TYPE").SortIcon" arial- \n hidden="true"></i>\n\n <a asp-action="Index" asp-route- \n sortExpression="@sortModel.GetColumn("COURT_TYPE").SortExpression">\n @Html.DisplayNameFor(model => model.COURT_TYPE)\n </a>\n </th>\n\nyou can see above that you are binding model.Court_type.\nNow if you look at your model,\n //Cascade|Dropdown\n\n [ForeignKey("COURT_TYPE")]\n [Display(Name = "COURT_TYPE")]\n public int? COURT_TYPE { get; set; }\n public virtual COURT_TYPE Court_Types { get; set; }\n\nAs you can see Court_Type is INT so definitely it will display INT value. If you want to display name then you have get through Court_Types object.\nIn another way,\n public PaginatedList<LawReview> GetItems(string SortProperty, SortOrder sortOrder, string SearchText = "", int pageIndex = 1, int pageSize = 5)\n {\n List<LawReview> items;\n\n if (SearchText != "" && SearchText != null)\n {\n items = _context.LawReviews.Where(n => n.LAWREVIEW_ID.ToString().Contains(SearchText) || n.JUDGMENT_NAME.Contains(SearchText) || n.JUDGMENT_NAME_VERSUS.Contains(SearchText) || n.JUDGMENT_NAME_ADDITIONAL.Contains(SearchText) || n.Court_Types.Name.Contains(SearchText) || n.Judge_Names.Name.Contains(SearchText) || n.JUDGMENT_NUMBER.Contains(SearchText) || n.JUDGMENT_DATE.Contains(SearchText) || n.HEADNOTE.Contains(SearchText) || n.Judgment_Countries.Name.Contains(SearchText) || n.States.Name.Contains(SearchText) || n.Judgment_Languages.Name.Contains(SearchText) || n.Catchword_Lv1.Name_Lv1.Contains(SearchText) || n.Catchword_Lv2.Name_Lv2.Contains(SearchText) || n.Catchword_Lv3.Name_Lv3.Contains(SearchText) || n.Catchword_Lv4.Name_Lv4.Contains(SearchText) || n.VERDICT.Contains(SearchText))\n .Include(u => u.Court_Types)\n .ToList();\n }\n else\n items = _context.LawReviews\n .Include(u => u.Court_Types).Include(r => r.Judge_Names).Include(s => s.Judgment_Countries).Include(t => t.Judgment_Languages).Include(v => v.Catchword_Lv1).Include(w => w.Catchword_Lv2).Include(x => x.Catchword_Lv3).Include(y => y.Catchword_Lv4).Include(z => z.States)\n .ToList();\n\n items = DoSort(items, SortProperty, sortOrder);\n\n PaginatedList<LawReview> retItems = new PaginatedList<LawReview>(items, pageIndex, pageSize);\n\n return retItems;\n }\n\nHere in items, you can use select and get the name from courtType object which I believe would be in relation with your primary table.",


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