在Mvc中创建模型的属性时的异常(对象引用未设置为对象的实例) [英] Exception when creating property of a model in Mvc(Object reference not set to an instance of an object)

查看:67
本文介绍了在Mvc中创建模型的属性时的异常(对象引用未设置为对象的实例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i我正在创建一个模型来设置另一个模型的属性我的代码是:



这是我的基本型号代码



Hi,

i am creating a model that set property of another model my code is:

this is my base model code

public class BaseModel
   {
       public Guid LeadsId { get; set; }
       public Guid UserId { get; set; }
       public string Description { get; set; }
       public AddressModel addressModel { get; set; }
      
   }





这是我的地址型号代码





and this is my address model code

public class AddressModel
   {
       public string Street { get; set; }
       public string City { get; set; }
       public string ZipCode { get; set; }
       public string Country { get; set; }
       public List<SelectListItem> CountryList { get; set; }
       public List<SelectListItem> StateList { get; set; }
       public List<SelectListItem> CityList { get; set; }
       public AddressModel()
       {
           CountryList = new List<SelectListItem>();
           CityList = new List<SelectListItem>();
           StateList = new List<SelectListItem>();
       }





这是我的数字列表绑定代码



public ActionResult CreateLead()

{

LeadsModel model = new LeadsModel();

model.addressModel.CountryList = objAds.SelectCountryList() ;

返回查看(模型);

}

但它通过异常对象引用未设置为对象的实例



请帮助我....谢谢。



this is my code for countylist binding

public ActionResult CreateLead()
{
LeadsModel model = new LeadsModel();
model.addressModel.CountryList = objAds.SelectCountryList();
return View(model);
}
but it through exception Object reference not set to an instance of an object

Please Help me....Thanks.

推荐答案

由于你的AddressModel有一个初始化程序,你需要创建它的一个实例,以便正确使用它。



Since your AddressModel has an initializer, you'll need to create an instance of it in order to use it properly.

public class BaseModel
   {
       public Guid LeadsId { get; set; }
       public Guid UserId { get; set; }
       public string Description { get; set; }
       public AddressModel addressModel { get; set; }

       // add default constructor that initializes addressModel
       public BaseModel()
       {
           addressModel = new AddressModel();
       }
      
   }


我通过创建AddressModel实例来解决它,但它创建了一个New实例HttpPost和HttpGet请求,我的查看结果是空的



请告诉我如何通过这种方法在控制器上发送数据。





I solve it by creating instance of AddressModel but it created a New instance both "HttpPost" and "HttpGet" request and my view result is empty

pls tell me how to send data on controller by this approach.


public class BaseModel
   {
       public Guid LeadsId { get; set; }
       public Guid UserId { get; set; }
       public string Description { get; set; }
       public AddressModel addressModel { get; set; }

       // add default constructor that initializes addressModel
       public BaseModel()
       {
           addressModel = new AddressModel();
       }

   }





[HttpPost]

公共ActionResult (BaseModel型号)

{

}

可能吗



[HttpPost]
public ActionResult(BaseModel model)
{
}
Is It Possible


这篇关于在Mvc中创建模型的属性时的异常(对象引用未设置为对象的实例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆