我是MVC的初学者,想知道如何通过绑定模型vaule进行查看。这是一个例子。
public class DataTypes
{
public Guid ItemID { get; set; }
[Required()]
public string Name { get; set; }
[Required()]
public string Status { get; set; }
[Required()]
public DataModel DataModel { get; set; } // This is for Binding
}
public class DataModel
{
public string Activity { get; set; }
public DateTime ?DateTime { get; set; }
}
使用上面的模型类,我可以成功地将数据从UI绑定到后端,但问题是如何使用上面的方法将相同的数据重新绑定到UI。我尝试了下面的代码,但是在设置Binded类(DataModel)
的vaules时 this.dataType.ItemID = // Guid from stored vaule in DataBase
this.dataType.Name = // Name from stored vaule in DataBase
this.dataType.Status = // Status from stored vaule in DataBase
// Set the activity to UI - ERROR.....!!!!!!
// Error was NullReferenceException unhandled
this.dataType.DataModel.Activity = // Activity from stored vaule in DataBase
this.dataType.DataModel.DateTime = // DateTime from stored vaule in DataBase
return View(this.dataType);
是否有解决上述问题的方法?
预先谢谢, HV
答案 0 :(得分:1)
您似乎忘记了实例化this.dataType.Datamodel:
this.dataType.DataModel = new DataModel();
this.dataType.DataModel.Activity = // Activity from stored vaule in DataBase
this.dataType.DataModel.DateTime