Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I'm new in implementing asp.net core project. By using CRUD methodology I created a MVC project. In one of my Controller class called ApiApplicantController, in its Edit action, there is an expression like below:

ViewData["Apiid"] = new SelectList(_context.Api, "Id", "Id", apiapplicant.Apiid);

which is totally ambiguis for me to understand.
There is a table called Api that has the following fields: Id,name,lastVersion
Also there is another table called apiapplicant that has the following filed: ApiApplicantId, reqNo, Apiid which is fk to ApiID. Now I want the user when wants to edit a record, the selectList shows the names of Api not the Ids. Accordingly, How can I make that changes to the above SelectList?

What I have tried:

C#
ViewData["Apiid"] = new SelectList(_context.Api, "Id", "Id", apiapplicant.Apiid);
Posted
Updated 31-Jan-20 3:34am

1 solution

The SelectList constructor you're calling takes four parameters:
  • The list of items to display (_context.Api);
  • The name of the property on an item which provides the value of the list item ("Id");
  • The name of the property on an item which provides the text of the list item (also "Id");
  • The value of the selected item (apiapplicant.Apiid);

To change the text that's displayed in the list, you need to change the third parameter:
C#
ViewData["Apiid"] = new SelectList(_context.Api, "Id", "name", apiapplicant.Apiid);
SelectList Constructor (Microsoft.AspNetCore.Mvc.Rendering) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900