-1

I am getting a null reference exception on the below line:

foreach (var povList in @Model.HomePovsList)

This is the code in my view:

@if (@Model.HomePovsList != null)
{
    foreach (var povList in @Model.HomePovsList)
    {
            <li>
                 <img alt="" src="@FMAapp.Helpers.ConfigHelper.CDNHost@Url.Content(@FMAapp.Helpers.ConfigHelper.HomePovImage + @povList.ImageUrl)" />
                <div class="orbit-caption">

                    <h2 class="shrink">@Html.Raw(HttpUtility.HtmlDecode(@povList.Headine))</h2>
                    <h2 class="shrink"><strong>@Html.Raw(HttpUtility.HtmlDecode(@povList.Headline1))</strong></h2>
                    <p>@Html.Raw(HttpUtility.HtmlDecode(@povList.SubHeadline))</p>

                      @if (povList.IsExternal != null)
                      {
                         <a class="button" href="http://@povList.LinkTo" target="_blank"> @povList.ButtonText</a>
                      }
                      else
                      {
                           <a class="button" href="@Url.Action(@Model.GetHomeFeaturePovLink(povList.LinkTo).Action, @Model.GetHomeFeaturePovLink(povList.LinkTo).Controller, new { PCTR = @Model.PCTR })"> 
                            @povList.ButtonText</a>
                      }


                    <p><small>@povList.Disclaimer</small></p>
                </div>
        </li>
    }
}

In the controller I am assigning like this:

var currentPage = new PageProperties(PCTR) {
    HomePovsList = new HomePovRepository().GetHomePovImages(PCTR)
};

new HomePovRepository().GetHomePovImages(PCTR) returns an IList<Marketing_HomePov>.

In my view model, I have this property:

public IList<Marketing_HomePov> HomePovsList
    { get; set; }
Ry-
  • 218,210
  • 55
  • 464
  • 476
B_pati
  • 69
  • 2
  • 10

1 Answers1

0
  1. You don't need to use @Model, use Model instead.The @ is redundant here.(in if and foreach)
  2. Make sure you set your ViewModel correctly (@model IEnumerable<YourClass> for example)
  3. Make sure your method doesn't return null.
  4. Make sure you pass your model to the View when returning it from your Controller. (return View(model))
Selman Genç
  • 100,147
  • 13
  • 119
  • 184