Metadata and Linked Tables

When working with scaffolded forms and metadata in MVC there is one small hiccup that will typically catch most beginning programmers. When using metadata to change the displayed label for a data column from a table at times not all of the display names will take effect. There is one small easy fix for this.

Lets start out with a schema like the one below where we have a category table linked in a one to many relationship with the items table.

We then will go in and create the metadata for the items table to rename the ItemName column to display as “Item Name” and the CategoryID column to display as “Select Category”.

When we scaffold out Items and run the Create form we don’t immediately see “Select Category” however we do see “Item Name” so we know that the metadata class was attached to the entity correctly.

The issue is that when Create and Edit forms are scaffolded and there are linked tables the scaffolding adds a hard coded label into the Html.LabelFor field. This field overrides the metadata value and does not allow it to display.

We simply need to delete this value and the following comma, save and refresh the page. Then the display value from the metadata will be displayed as the label as desired.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *