Obtaining the field name from a SQL Server database by utilizing the field ID

When viewing my selections, I have several drop-down lists that display the ID and I would like the name to be shown to the user for clarity. However, when I list everything that has been added, it displays the chosen ID instead of the name.

I attempted to resolve this by using the code below, but it always displays the status with the ID 1, which is "Active."

@Html.DropDownListFor(modelItem => item.status,
                    (SelectList)ViewBag.listStatus,
                    new { @class disabled = "" })

//controller

    {
        public ActionResult listStatus()
        {
            var list= ListsDAO.listStatus();
            return View(list);
        }
    }

//DAO

{
    public static IEnumerable<LISTS> listStatus()
    {
        using (var ctx = new PadraoEntities())
        {
            var result= from lists in ctx.LISTS
                            where lists.TYPE_LIST== "STATUS"
                            select new
                            {
                                lists.IDE_LIST,
                                lists.DES_LIST,
                                lists.VLR_LIST
                            };
            var list= new List<LISTS>();
            foreach (var item in result)
            {
                list.Add(new LISTS()
                {
                    IDE_LIST = item.IDE_LIST,
                    VLR_LIST = item.VLR_LIST,
                    DES_LIST = item.DES_LIST
                });
            }
            return list;
        }
    }
}

//view add

<div class="form-group">
@Html.LabelFor(model => model.status, htmlAttributes: new { @class = "control -Label col-md-2" })
  <div class="col-md-10">
    @Html.DropDownListFor(model => model.status(SelectList)ViewBag.listStatus, new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.status, "", new { @class = "text-danger" })
  </div>
</div>

//view list

 {
    @Html.DropDownListFor(modelItem => item.ststus,(SelectList)ViewBag.listStatus,new { disabled = "" })
    }

Instead of displaying the IDs when listing, I expected "Active" to appear for ID 1, "Inactive" for ID 2, and so on.

Answer №1

Give this a try //controller

public ActionResult displayStatus()
    {
        ViewBag.Status = new SelectList(//Code to retrieve id and its attribute, "StatusId", "Status");
        var list= ListsDAO.listStatus();
        return View(list);
    }

//Display in View

<div class="form-group">
            @Html.LabelFor(model => model.Status, "TaskId", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("Status", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })
            </div>
        </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with charts not appearing on your Django website?

I am working on a Django project where I need to integrate bar-charts using Django-nvd3. Although I have successfully displayed the bar-charts in separate projects, I am facing an issue with integrating them into my current project. Below is the code snipp ...

Is the drag-and-drop feature not working properly?

I'm new to coding in Javascript and I'm attempting to insert an image inside a border created with CSS. Unfortunately, the script doesn't seem to be working properly. It's possible that there is a small error in the code causing the iss ...

Experiencing difficulties in accessing complete data from a PowerShell script while using powershell.Invoke() function

I am currently facing an issue where I am unable to retrieve the complete list of VM's hosted on a SCVMM machine. Only around 100-110 VM's are being displayed, whereas there should be more. I am fetching specific properties from this list and sto ...

Fiddle demonstrates HTML functionality, while local testing is unsuccessful

Currently, I am in the process of creating an image slider and attempting to run it on my personal computer. However, upon doing so, I have encountered a problem where the page is not rendering correctly. Additionally, I receive an ActiveX warning message ...

Upon calling the createModalAddPost() function, a single window is triggered to open

Hey there, I'm having a JavaScript question. So, I have a panel that opens a window, and it works fine. But the issue arises when I close the window and try to open it again - it doesn't work. I have to reload the page every time in order to open ...

Learn how to display two different videos in a single HTML5 video player

Seeking a solution to play two different videos in one video element, I have found that only the first source plays. Is jQuery the answer for this problem? HTML Code: <video autoplay loop id="bbgVid"> <source src="style/mpVideos/mpv1.mp4" type ...

Cannot access PDF files on Android Chrome

I'm using a responsive bootstrap application and encountering an error when trying to open a PDF on mobile. I can open the PDF from my iPhone, but when trying to do so on Android Chrome, I receive a "media not supported exception" message. The applic ...

What is the best way to store an HTML header text file in a PHP variable?

Here is a snippet of my HTML file: <form action="cnvrt.php" method="POST" > Enter your text here - <input type="text" id="in" name="in"> <br> <input type="submit" value="submit" > </form> This is how my PHP file look ...

What is the best way to insert a Bootstrap Glyphicon into an asp:TextBox within ASP.Net Webforms?

Can a Glyphicon be displayed in an <asp:TextBox> control? I attempted the following: <div class="col-md-10 has-feedback"> <asp:TextBox runat="server" ID="txtFullName" CssClass="form-control" MaxLength="50" /> <i class="glyph ...

Reorganizing and maintaining column sequence in gridview while preserving changes during postbacks

On my webpage, I have implemented a GridView to display records. Users are able to customize the view of the GridView by rearranging or adding/removing columns from a predefined list. During the OnPagePrerender event, I remove all existing cells from the G ...

Discover the contents within #document utilizing PUPPETEER

Can someone assist me as I am new here? I am trying to retrieve elements from a virtual reference #document in HTML using Puppeteer, but every time I attempt it, I receive an error stating that the element does not exist. Here are some examples: HTML &l ...

The ins and outs of asynchronous programming with Async Await and tips on circumventing the

I need to extract 100 pages from a specific URL structure. The URL format is as follows: My program uses a for loop to cycle through these pages and save the output in an HTML file on my local storage. . . I have a query about my code, is there a more ...

Page jumping vertically in Chrome upon reload, with Firefox and Internet Explorer functioning properly

Utilizing this jQuery script, I am able to center a website vertically within the browser window if it exceeds the height of the outer wrapper-div, which has fixed dimensions. $( document ).ready(function() { centerPage(); )}; // center page vertic ...

Ensure the background image is optimized for viewing on screens of any size

I am facing an issue with an image on my website that serves as the background for some elements. The image is wider than it is tall, causing problems for users viewing the site on phones. Is there a way to cut off the sides of the image and ensure it fits ...

"Stuck: CSS Div Transition Effect Refuses to Cooper

I am looking to incorporate a transition on my div when it is clicked. I've tried using jQuery with this example: http://jsfiddle.net/nKtC6/698/, but it seems to not be working as expected. So, I want to explore another example using CSS: http://jsfid ...

"Hiding elements with display: none still occupies space on the

For some reason, my Pagination nav is set to display:none but causing an empty space where it shouldn't be. Even after trying overflow:hidden, visibility:none, height:0, the issue persists. I suspect it might have something to do with relative and ab ...

Expanding the table to display additional rows using pagination in an Angular application

I have implemented a table in Angular that displays data fetched from an API. The requirement is to initially display only the first 5 rows and provide an option for users to view more rows (in groups of 5) with pagination support. Below is the code snipp ...

What is the best way to connect a VueJS event to a C# razor input field?

I have a series of dropdown menus created using razor html helpers: @Html.DropDownList("formtype-filter", Model.FormTypes, "Any Type...", new { @class = "form-control" }) I am looking to integrate a 'change' event listener to this input using ...

Is there a way to conceal a div element if the user is not logged in?

I have been enlisted to assist my friend with a project involving his website. Within the site, there is a checkbox that prompts the display of a div when selected. I believe it would be more effective for this checkbox to only be visible to users who are ...

Alter the class generated by ng-repeat with a click

I currently have a dynamically generated menu displayed on my website, and I am looking to apply a specific class to the active menu item based on the URL (using ngRoutes). The menu items are generated from a $scope.menu object, so my initial thought was t ...