Duplicate String values

Duplicate object values in ASP.NET MVC Display Templates? Easy fix!

This post was most recently updated on July 31st, 2022.

< 1 min read.

Are you getting duplicate object values (or whatever those Objects output in .ToString())? Eh, so was I, after I edited the display template for String. Found a fix, though.

Description

Okay – I just ran into one of my more stupid mistakes since.. Well, since forever.

I had made some quick and simple edits in String.cshtml display template (among quite a few other edits before building again and seeing what happened), as I added support for Enum values there. After that, I started getting duplicate values for String-typed properties. 

Apparently, mistakes were made.

Solution

Luckily, this was easy to fix (but probably still worth documenting):

I had added the code for handling Enum-values, and also changed “@model String” -declaration to awful “@Model object” -one. Changing the type was supposed to happen, but capitalizing the model messed up my DisplayTemplate. Instead of casting the model to object, I was simply calling Model at the start of the template (basically, that was shorthand for Model.ToString()), hence the “String object String”-kind of duplicate values on the display template.

MVC Model object
Instead of this (or similar with String replacing object)…

Okay, so that resulted in me having weird duplicate values.

This, however, works as it should:

MVC model object
You should have this – or something similar!

Fast and easy fix, but hope this helps someone!

mm
4 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments