所以我有一个MVC程序,它将View表单中的信息发送到XML文件。现在这些信息写得正确但每次都会因为以下错误而导致中断,我无法理解为什么!
以上是我在ScorecardRepository.cs中的构造函数,以下是我的模型的代码,即Scorecard.cs
public partial class Scorecard
{
public int ScorecardID { get; set; }
public int VendorID { get; set; }
public string Title { get; set; }
public bool Enabled { get; set; }
public System.DateTime Created { get; set; }
public int CreatedBy { get; set; }
public Nullable<System.DateTime> LastUpdated { get; set; }
public Nullable<int> UpdatedBy { get; set; }
public Scorecard()
{
this.ScorecardID = 0;
this.VendorID = 0;
this.Title = null;
this.Enabled = true;
this.Created = DateTime.Now;
this.CreatedBy = 0;
this.LastUpdated = DateTime.Now;
this.UpdatedBy = 0;
}
public Scorecard(int scorecardid, int vendorid, string title, bool enabled, DateTime created, int createdby, DateTime lastupdated, int updatedby)
{
this.ScorecardID = scorecardid;
this.VendorID = vendorid;
this.Title = title;
this.Enabled = enabled;
this.Created = created;
this.CreatedBy = createdby;
this.LastUpdated = lastupdated;
this.UpdatedBy = updatedby;
}
}
这是我的XML文件样本,其中写入了数据!
任何人都可以帮助我吗?我的价值不能为空吗?
答案 0 :(得分:2)
您的item
个元素之一不包含您要查找的所有元素。
当您尝试将(XElement)null
投射到不可为空的值类型(例如int
或DateTime
时)时会引发异常:
XElement t = null;
int v = (int)t;
可能的解决方案?将您的属性更改为Nullable<T>
或确保您尝试转换为值类型的元素确实存在于XML文档中。