0

I'm getting a null reference error with my dropdownlistfor in my partial view. I can't seem to find what I'm missing or doing wrong to get an empty object reference. ModelCode:

CommonHerpNames.cs

public class CommonHerpNames
    {
        [Key]
        public int CommonHerpId { get; set; }

        public string HerpName { get; set; }
}

NewReptileViewModel.cs

public class NewReptileViewModel
{
    public IEnumerable<CommonHerpNames> CommonAnimalNames { get; set; }
    public ReptileModel ReptileModel { get; set; }
}

Controller Code:

  public ActionResult _AddReptile()
    {
        var commonAnimalTypes = _context.CommonHerps.ToList();
        var viewModel = new NewReptileViewModel
        {
           CommonAnimalNames = commonAnimalTypes
        };            

        return PartialView("_AddReptile", viewModel);
    }

Partial ViewCode:

<div class="form-group">
     @Html.DropDownListFor(model => model.ReptileModel.CommonHerpTypeId, new SelectList(Model.CommonAnimalNames, "CommonHerpId", "HerpName"), "Select Animal")
</div>

Main View Code:

<div>
    @Html.Partial("_addReptiles")
</div>

For the record, I know my CommonHerps context is good as I recently completed an Add-Migration and Update-Database -Verbose.

Alex
  • 443
  • 3
  • 18
  • You are not creating an instance of ReptileModel in your view model. You are dereferencing ReptileModel to get the Id in your view, but ReptileModel is null. You probably want to create an instance of the ReptileModel in your NewReptileViewModel constructor. – Erik Funkenbusch May 14 '17 at 20:35
  • How am I not. I'm referencing it after ienumerable – Alex May 14 '17 at 21:02
  • How are you calling this partial view? –  May 14 '17 at 22:01
  • Using @html.partial("_addReptiles") between a set of did tags in my main page – Alex May 14 '17 at 22:04
  • 1
    @Alex - You are *DECLARING* ReptileModel, you are not *INSTANTIATING* it. You are creating a variable, of type ReptileModel which is null by default. Do what I said, and create a new instance in your constructor. – Erik Funkenbusch May 14 '17 at 22:43
  • 1
    `@Html.Partial()` does not call a controller method - you need `@Html.Action()` for that (yo need t add the relevant code in your question) –  May 15 '17 at 02:15
  • Thank you @StephenMuecke If you want to make your comment the answer I will mark it. That solved my issue! – Alex May 15 '17 at 03:54

1 Answers1

1

Using Html.Partial() does not call your server method. It just renders the partial view (passing the model in the main view to the partial) and the error is thrown because CommonAnimalNames is null and you cannot initialize a new SelectList for a null.

Change the code in the main view to

@Html.Action("_AddReptile") // add controller name as 2nd parameter if necessary

or

@{ Html.RenderAction("_AddReptile"); }

which will call your _AddReptile() method, which will in turn initialize your view model and set the value of CommonAnimalNames.

You may also want to consider marking _AddReptile() with the [ChildActionOnly] attribute to prevent it being called by the browser.