简体   繁体   English

在MVC中填充我的模型时出现错误“指定的转换无效”

[英]Error on filling my model in MVC “Specified cast is not valid”

I have this model 我有这个模特

 public class DocumentModel
    {   
        public int documentID { get; set; }
        public String Title { get; set; }
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
        public DateTime DateCreated { get; set; }
        public String Description { get; set; }
        public int authorID { get; set; }
        public String AuthorName { get; set; }
        public int categoryID { get; set; }
        public String Category { get; set; }
        public int topicID { get; set; }
        public String Topic { get; set; }
        [AllowHtml]
        public String DocumentBody { get; set; }
    }

When im about to fill my Model after making a query in mysql here's the code 当我要在mysql中查询后填充我的模型时,这里是代码

 result = from DataRow row in data.Rows
                         select new DocumentModel()
                         {
                             documentID = (int)row["document_id"],
                             Title = row["title"].ToString(),
                             DateCreated = (DateTime)row["date_created"],
                             Description = row["description"].ToString(),
                             AuthorName = row["AuthorName"].ToString(),
                             categoryID = (int)row["category"],
                             topicID = (int)row["topicID"]
                         };

Im having an error in the CategoryID and TopicID the error says "Specified cast is not valid." 我在CategoryID和TopicID中存在错误,错误消息为“指定的转换无效。” i dont know where did i get that error i already check all the datatypes if its correct and i confirm that its all correct could you give me advice for this? 我不知道我在哪里得到该错误的,我已经检查了所有数据类型是否正确,并且我确认所有数据类型都正确,您可以为此提供建议吗? did i miss some codes of wrong implementations? 我是否错过了一些错误的实现代码?

before i forgot here is my Query code for MYsql 在我忘记这里之前,这是我的MYsql查询代码

select a.document_id,a.title,date(a.date_created) as date_created,a.Description,c.first_name as AuthorName,ifnull(e.category_ID, 0) as category, ifnull(d.ID,0) as TopicID from tbldocument a
Inner join tbldocumenttree b on a.document_ID = b.Document_ID
left join tblcategory e on b.category_id = e.category_id
Inner join tblauthor c on a.author_id = c.author_id
left join tbldocumenttree d on d.ID = b.parent

Thank you. 谢谢。

This can happen if row["category"] (same applied for row["topicID"]) is NULL in the data row because you can't cast null to non-nullable int. 如果数据行中的row [“ category”](与row [“ topicID”]相同)为NULL,则会发生这种情况,因为您不能将null强制转换为不可为null的int。 I notice that you are left joining your tblCategory and therefore NULL is a real possibility. 我注意到您离开了加入tblCategory的行列,因此NULL的可能性很大。

Either fix on the server using a coalescing function (ie to force any NULL category to default to 0) or else check if row["category"]==DBNull.Value before you attempt to cast it, or indeed use Int32.TryParse and have whatever handling for a category of 0, etc. 使用合并函数在服务器上修复(即,将任何NULL类别强制为默认值设置为0),或者在尝试强制转换前检查row [“ category”] == DBNull.Value,或者确实使用Int32.TryParse和对类别0进行任何处理,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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