Click here to Skip to main content
15,115,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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

this is my base model code

XML
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

SQL
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>();
       }


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.
Posted
Updated 30-Jul-14 3:14am
v2

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

C#
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();
       }
      
   }
   
Comments
Yogesh Kumar Tyagi 30-Jul-14 8:46am
   
Thanks... But on "HTTPPOST" the AddressModel value empaty
Yogesh Kumar Tyagi 30-Jul-14 8:58am
   
can i used this for both "HTTPGET" and "HTTPPOST"
Nathan Minier 30-Jul-14 9:08am
   
I just realized, we haven't seen the code for your LeadsModel, is there anything else in there which might be affecting things?

Also, can I see your View, as it's entirely likely the issue is in there.
Yogesh Kumar Tyagi 30-Jul-14 9:24am
   
i am calling partial view for address information

<div>@{Html.RenderPartial("~/Areas/Leads/Views/Address/_Address.cshtml", Model.addressModel);}</div>
but your approach work nice on "HttpGet" Request but on httpPost it will create a new instance of AddressModel so i m not able to save data pls help
Nathan Minier 30-Jul-14 10:07am
   
If I had to guess, without seeing the code for the view, I'd say that whatever posting mechanism you're using to send back to the controller is not aware of what's in your partial view. I would strongly suggest either rolling the address information form into the View that you're using, or creating an editor template for the address fields if you plan to use that object with different views.

Yogesh Kumar Tyagi 30-Jul-14 10:10am
   
Thanks Dear ....for help
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.


SQL
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]
public ActionResult(BaseModel model)
{
}
Is It Possible
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900