SQLクエリを実行し、モデルタイプのリストをビューに戻すコントローラアクションがあります。ビューは、各モデルに含まれるデータを含むシンプルな表を出力します。MVCモデルにnull datetimeを表示する方法
マイモデル:
public class UserModel
{
[Key]
public string UserID { get; set; }
public string EmailAddress { get; set; }
public string Password { get; set; }
public string SecurityToken { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime CreatedDate { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime LastUsedDate { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime LastUpdatedDate { get; set; }
}
マイビュー:データ内の一部の行については
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmailAddress)
</td>
<td>
@Html.DisplayFor(modelItem => item.SecurityToken)
</td>
<td>
@Html.DisplayFor(modelItem => item.CreatedDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastUsedDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastUpdatedDate)
</td>
</tr>
}
は、LastUsedDateとLastUpdated日付がnullであるので、彼らは1900-01-01に頼る、そして彼らそのように表示されます。
1900-01-01の場合、この値を表示しない方法はありますか?コントローラで
私のクエリは
SELECT UserID, EmailAddress, SecurityToken, CreatedDate,
ISNULL(LastUsedDate, '1900-01-01') AS LastUsedDate,
ISNULL(LastUpdatedDate, '1900-01-01') AS LastUpdatedDate
FROM table1 ORDER BY CreatedDate DESC
であると私はそうのようなモデルに割り当てる:NULL可能日時(DateTime?
)に
var user = new UserModel
{
CreatedDate = (DateTime)reader["CreatedDate"],
LastUsedDate = (DateTime)reader["LastUsedDate"],
LastUpdatedDate = (DateTime)reader["LastUpdatedDate"]
}