简体   繁体   中英

ASP.NET DateTime format on IDictionary<T>

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;
 <tr>
 @foreach (var col in Model.Columns)
   { <td class="small">@dict[col.COLUMN_NAME]</td> }
 </tr>

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.

if (dict[col.COLUMN_NAME] is DateTime)
  <td>@(((DateTime)dict[col.COLUMN_NAME]).ToShortDateString())</td> 

There's a lot of typing issues going on here. I think your trouble is stemming from calling ToString on an object , rather than a DateTime I'll take a stab at correcting it... Try this:

@foreach (IDictionary<string, DateTime> dict in Model.SampleData)
{
 <tr>
 @foreach (var col in Model.Columns)
   { <td class="small">@dict[col.COLUMN_NAME].ToString("dd/MM/yyyy")</td> }
 </tr>

The key here is to alter your dictionary to IDictionary<string, DateTime> . The reason is that object also has a ToString method defined, however it will not format your date for you. If you change your dictionary's signature then your indexer will return a DateTime instead of an Object , and the correct ToString method will be called.

If you cannot use this syntax for reasons not shown, you can accomplish this with a cast as well.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM