0
votes

I am using this example to develop a dropdownlist. It works well until I make some changes in the way my model is called within my view. The dropdownlist model class is called dropdownModel. Because my view contains 2 models, I created a "big" model class, BigModelClass that holds my two models.

The big model looks like this

public class BigModelClass {
   public DropDownModel dropDownModel { get; set; }
   public IEnumerable<projectname.Model.model2> var2 { get; set; }
}

In my view, I call the model as:

@model BigModel

Now in my view, I call use the dropdownlist as follows:

@Html.LabelFor(m => m.dropDownModel.State)
@Html.DropDownListFor(m => m.dropDownModel.State,
                 new SelectList(Model.dropDownModel.StateList, "Value", "Text"))
<span class="required"></span>
@Html.ValidationMessageFor(m => m.dropDownModel.State)

Unfortunately, I get the following error:

System.NullReferenceException: Object reference not set to an instance of an object.

on the line

@Html.DropDownListFor(m => m.dropDownModel.State, new SelectList(Model.dropDownModel.StateList, "Value", "Text"))

Averything works fine if I use only the dropDownModel model.

Any help is very appreciated

EDIT The controller for the view:

public ActionResult Index(){
   return View (new BigModelClass());
}
3
Post the code for your controller, the problem may be how you are instantiating the model.Johann
you are using m.dropDownModel but i can't see definition for dropDownModel in BigModelBehnam Esmaili
@Johann & Behnam: Please see edit.jpo
@jpo where you initialize the Model.dropDownModel ?Behnam Esmaili
you'r exact problem is this!you can initialize it in the constructor.Behnam Esmaili

3 Answers

2
votes

Assuming you copied DropDownModel directly from that example, you need to add a constructor to BigModelClass and instantiate the dropDownModel there.

public class BigModelClass {
   public DropDownModel dropDownModel { get; set; }
   public IEnumerable<projectname.Model.model2> var2 { get; set; }

   public BigModelClass() {
      dropDownModel = new DropDownModel();
   }
}

or, in your controller, instantiate the dropdownmodel:

public ActionResult Index(){
   return View (new BigModelClass {
         dropDownModel = new DropDownModel()
   });
}
1
votes

Most likely your Model.dropDownModel is null, I'm quite sure you don't instantiate it in your default constructor BigModelClass(). And when it's ok for property definition m => m.dropDownModel.State it fails to return instance of items collection: Model.dropDownModel.StateList

0
votes

This problem is happened because you did not bind data to dropdownlist. You have to bind data in to dropdownlist in your controller action. If you are binding data on your controller action then be sure that it also bind in [httppost] controller action for modelstate.valid is false.

    public ActionResult Register()
    {
        RegisterModel model = new RegisterModel();
        List<SequrityQuestion> AllSequrityQuestion = new List<SequrityQuestion>();
        model.SequrityQuestions = GetAllSequrityQuestion();

        return View(model);
    }

     [HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        if (!ModelState.IsValid)
        {
            // there was a validation error =>
            // rebind categories and redisplay view

            model.SequrityQuestions = GetAllSequrityQuestion();
        }
        if (ModelState.IsValid)
        {
            // Your code to post
        }

        return View(model);
    }

In above example there is dropdownlist in register model named SequrityQuestions. I think this is why you faced that problem. be sure to bind data to dropdownlist in case of modelstate.valid false, then your problem will be gone.