这个错误让我发疯。它曾经工作过。现在没有了。不知道是什么变化导致了它,我不能在不丢失很多东西的情况下回滚。
Unable to cast object of type 'System.Collections.Generic.List`1[Literrater.Models.ranges]'
to type 'Literrater.Models.ranges'.
这是模型。
public class Comment
{
[Key]
public int CommentID { get; set; }
public int ProjectDocID { get; set; }
public int UserID { get; set; }
public string text { get; set; }
public string quote { get; set; }
public DateTime DateCreated { get; set; }
public virtual ICollection<CommentVote> CommentVote { get; set; }
public virtual ICollection<CommentReply> CommentReply { get; set; }
public virtual ICollection<CommentReport> CommentReport { get; set; }
public ProjectDoc ProjectDoc { get; set; }
public virtual User User { get; set; }
public ICollection<ranges> ranges { get; set; }
}
public class ranges
{
public int ID { get; set; }
public string start { get; set; }
public string end { get; set; }
public string startOffset { get; set; }
public string endOffset { get; set; }
}
引发错误的操作
[HttpPost, ActionName("Create")]
public ActionResult Create(Comment comment)
{
comment.DateCreated = DateTime.Now;
comment.UserID = WebSecurity.CurrentUserId;
db.Comments.Add(comment); //Error here
db.SaveChanges();
return Json(comment);
Json 被发布。
{"text":"",
"ranges":
[{
"start":"/p[1]",
"startOffset":160,
"end":"/p[1]",
"endOffset":234
}],
"quote":"es. Hadn't they ever heard of potpourri? Each incessant beep, made by the",
"UserID":"6",
"ProjectDocID":"1"
}
更新:旧工作数据库的屏幕截图