[英]Date is 01/01/0001 and not the date that is in the database?
I have this line in my sql server 2008 r2 database, but I don't understand: 我在我的sql server 2008 r2数据库中有这一行,但我不明白:
2012-12-06 11:00:36.703 and is of type DATETIME
When I display it in my view it is: 当我在我的视图中显示它时:
Mon, Jan 1, 0001
Here my code: 这是我的代码:
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; }
}
Can some one help me, please? 有人能帮助我吗?
This is the problem: 这就是问题:
news.Add(new DataModels.News.NewsPostsDataModel { NewsPostId = id ,Message = message});
You're not populating CommentDate
here, so it's got the default value of DateTime.MinValue
. 你没有在这里填充
CommentDate
,所以它有默认值DateTime.MinValue
。
Of course we now can't tell whether your stored procedure returns the value at all, but assuming it does, you should presumably be fetching it from the DataRow
and putting it in your model... 当然,我们现在无法判断您的存储过程是否完全返回值,但假设它存在,您应该从
DataRow
获取它并将其放入模型中...
Before you just fix the code, you should take a step back and think about your diagnostic process. 在你刚修好的代码,你应该退一步,想想你的诊断过程。 Did you try debugging into this?
你试过调试吗? Adding logging?
添加日志? You should have been able to see very quickly that you didn't have a
CommentDate
in the model, and then tried to work out why you didn't have a CommentDate
, which should have led you back to the code creating the instance of the model. 您应该能够非常快速地看到模型中没有
CommentDate
,然后尝试找出为什么没有CommentDate
,这应该会让您回到创建实例的代码模型。 If you can improve your diagnostic processes, you can speed you your future development significantly by not having to ask as many questions. 如果您可以改进诊断过程,则可以通过不必提出尽可能多的问题来显着加快您未来的发展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.