模型绑定以在 MVC 中查看
我是 MVC 的初学者,想知道如何通过绑定模型值设置回来以供查看。这是一个例子。
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。我尝试了下面的代码,但是当涉及到设置绑定类(DataModel)的值时,
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);
有解决上述问题的方法吗?
预先感谢, 高压
I am a beginner to MVC and would like to know how I can set by binded model vaule back for viewing. Here is the example.
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; }
}
With the above model class, I am sucessfully able to bind data from UI to backend but the problem is that how I can retrun the same data to UI using the above. I tried the below code but when it comes to setting the vaules for Binded class (DataModel)
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);
Any work around for the above issue?
Advance Thanks,
HV
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎忘记实例化 this.dataType.Datamodel:
It appears that you forgot to instantiate this.dataType.Datamodel: