我的 sql server 2008 r2 数据库中有这一行,但我不明白:
2012-12-06 11:00:36.703 and is of type DATETIME
当我在视图中显示它时,它是:
Mon, Jan 1, 0001
这是我的代码:
var allNews = ZincService.NewsService.GetNewsPostsForId(id);
List<Zinc.Web.Areas.News.ViewModels.Home.NewsPostsViewModel> newsItems = new List<NewsPostsViewModel>();
foreach (var newsItem in allNews)
{
NewsPostsViewModel newsItemViewModel = new NewsPostsViewModel();
newsItemViewModel.CommentDate = String.Format("{0:ddd, MMM d, yyyy}", newsItem.CommentDate);
}
public class NewsPostsViewModel
{
public Entities.News.News MainNews { get; set; }
public virtual string News { get; set; }
public virtual int NewsId { get; set; }
public virtual string CommentDate { get; set; }
}
public List<DataModels.News.NewsPostsDataModel> GetNewsPostsForId(int id)
{
using (SqlConnection conn = new SqlConnection(ZincModelContainer.CONNECTIONSTRING))
{
using (SqlCommand cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "[News].[GetNewsPostsForId]";
SqlParameter param = new SqlParameter("@Id", System.Data.SqlDbType.Int);
param.Value = id;
cmd.Parameters.Add(param);
List<DataModels.News.NewsPostsDataModel> news = new List<DataModels.News.NewsPostsDataModel>();
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
foreach (DataRow row in dt.Rows)
{
string message = Convert.ToString(row["NewsDescription"]);
news.Add(new DataModels.News.NewsPostsDataModel { NewsPostId = id ,Message = message});
}
}
return news;
}
}
}
public class NewsPostsDataModel
{
public virtual int NewsPostId { get; set; }
public virtual string Message { get; set; }
public virtual DateTime CommentDate { get; set; }
}
有人能帮助我吗?