first post.

I'm creating a DB portal in ASP.NET MVC for people accessing my DB to be able to get a list of DBs, tables, sizes, etc.

In my View, I'm iterating over a dynamic list that contains SQL TOP 100 query results (rows of data) using a foreach loop within another foreach loop that iterates over a list of SQL Column headings; this part works fine. The issue I'm having is that for any of the DateTime results, I want to shorten them. I've been searching for 2 hours, but the solutions aren't doing it. If I was simply referencing @item I feel like it would work, but I think the IDictionary<> is causing the trouble. Here's the working loop:

@foreach (dynamic item in Model.SampleData)
 IDictionary<string, object> dict = item;
 @foreach (var col in Model.Columns)
   { <td class="small">@dict[col.COLUMN_NAME]</td> }

That works fine. But I want any @dict[col.COLUMN_NAME] values that are DateTime to have a short date format. I used an if statement to filter DateTime values (which works), but cannot get the right synatax to actually format the date correctly. I've tried the following based on examples from this site, but all fail:

   if (dict[col.COLUMN_NAME] is DateTime)
     <td>@dict[col.COLUMN_NAME].ToShortDateString()</td> //FAIL
     <td>@dict[col.COLUMN_NAME].ToString("dd/MM/yyyy")</td> //FAIL
     <td>@dict[col.COLUMN_NAME].Date.ToString("dd/MM/yyyy")</td> //FAIL

Given that it's a List there are no Model properties for me to [decorate]. I'd like to keep the loop the way it is, just figure out how to format the dict[col.COLUMN_NAME].

Thanks in advance.

Related posts

Recent Viewed