MVC Modelbinding和Saving对象

时间:2016-01-29 13:09:00

标签: c# asp.net-mvc model-binding

我对MVC编程很新,并测试了一些东西。 所以我用一些参数(id,...)和

创建了一个客户类
 public Address Address { get; set; } 
 //that is a Address Class with Addressid and streetname, ...

对于创建部分,我创建了一个只包含

的视图模型
public Customer custumor = new Customer()

将在de Create cshtml中用作强模型

现在在看了一些模型绑定之后,我发现我需要使用像

这样的东西
obj.customer.LastName = objContext.Request.Form["customer.LastName"];

从cshtml获取信息到我的对象中,它就像存储在Customer类中的所有东西的魅力一样。 现在我尝试添加来自

的信息
obj.customer.Address.StreetName = objContext.Request.Form["customer.Address.StreetName"];

存储到Address中它给我一个“未设置为对象实例的对象引用”nullrefereceException。 表单本身是正确的,因为我也尝试了

obj.customer.Address.StreetName = "Test"; 

并且给出了同样的错误。

我缺少什么?

谢谢!

0 个答案:

没有答案
相关问题