Home Displayed SQL dB data is from foreign key, not primary key
Reply: 0

Displayed SQL dB data is from foreign key, not primary key

user2803 Published in July 17, 2018, 3:24 pm

The ID passed to my foreign key (FK) seems to be the same ID passed from the primary key (PK).

Imagine a page that will act as a dashboard for various pieces of equipment. An IT dashboard for PC's, perhaps. I've set it up to display coloured tiles - each tile representing the operations "status" of that equipment/PC, and each tile represent a single piece of equipment. There are ~100 rows in the Equipment table, and 18 possible status types in StatusType. I have the database set up as follows:

Relational Database Diagram, MSSQL Server 2016

The models are defined as follows:

   public class BaseEntity {
    public int ID { get; set; }

public class StatusType : BaseEntity {
    public string UseCase     { get; set; }
    public string Description { get; set; }
    public string FillColour { get; set; }
    public string LineColour { get; set; }
    public string TextColour { get; set; }
    public bool   IsActive    { get; set; }


public class Equipment : BaseEntity {
    public         string     ModelNumber  { get; set; }
    public         string     Description  { get; set; }
    public virtual StatusType StatusTypeID { get; set; }


I am including @model IEnumerable<StackOverfulTestProject.Data.Entities.Equipment> in my view, and the implementation is as follows:

<div class="container body-content">

    <table class="Test Table"> 
        <tbody class="test-1">

            @foreach (var item in Model) {
                    <td> @Html.DisplayFor(modelItem => item.Description)</td>
                        @Html.DisplayFor(modelItem => item.StatusTypeID.FillColour)
                        <div class="metr">
                            <a style="cursor: pointer; border:6px solid #@Html.DisplayFor(modelItem => item.StatusTypeID.LineColour); background:#@Html.DisplayFor(modelItem => item.EquipmentStatusID.FillColour)" class="squareStyle" href="">
                                <span style=" color:#@Html.DisplayFor(modelItem => item.StatusTypeID.TextColour); font-size:  1.1em; float: right; margin-top: 5px; padding-right: 0.2em">xx</span> </a>



Final Displayed Table - Incorrect

This should be a table with ~100 tiles of a single status assigned. Instead, it is printing across the FK, and not doing a lookup of the ID number from the PK.

Any help would be greatly appreciated.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.312111 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO