1

I'm trying to understand how to make partial views. So far I have the following for the partial view, called "_News":

@model Site.Services.News.NewsItem

<div class="bs-callout bs-callout-primary">
        <h2>
            @Html.DisplayFor(model => item.Title)
        </h2>
    </div>

And then in the controller I have:

@model IEnumerable<Site.Services.News.NewsItem> - Does this belong here?

...other controller code here...

@foreach(var item in Model)
{
    Html.Partial("_News", item);
}

But I'm getting "NullReferenceException" when I try to run the application. What am I doing wrong?

Edit as per comments:

        public ActionResult Index()
    {
        NewsReader newsReader = new NewsReader();
        var newsItems = newsReader.GetNewsItems();


        return View(newsItems);
    }
user9993
  • 5,833
  • 11
  • 56
  • 117
  • You need to show your controller code (the 2nd code snippet is not your controller code - its the view code) –  Jun 03 '15 at 00:03
  • Can you show us the value in Message property in your exception – Júlio Murta Jun 03 '15 at 00:03
  • No its controller code see edit. There is also no Message property. – user9993 Jun 03 '15 at 00:09
  • You still have not shown the controller code (that's the view code!). Show the method that generates the view - i.e. `public ActionResult Something() { ... }` in your controller –  Jun 03 '15 at 00:11
  • Most likely `IEnumerable` is null. How do you assign that value in controller? – Win Jun 03 '15 at 00:11
  • Sorry, I got confused. I've got the controller in the post now. The whole NewsReader part works fine as I have a specific page for displaying news items and that works correctly, but I'm trying to put a news section within another page by making a partial view to just display the title instead. It just seems to be a problem with using partials? – user9993 Jun 03 '15 at 00:16

2 Answers2

3
@model Site.Services.News.NewsItem

<div class="bs-callout bs-callout-primary">
     <h2>
         @Html.DisplayFor(model => model.Title)
     </h2>
 </div>

There is a syntax error in your code, it should read model.Title, not item.Title because you are referring to the the model as model in the lambda expression.

I.e. this is the same: @Html.DisplayFor(x => x.Title)

EDIT:

You also need to put an @ symbol before the Html.Partial

@foreach(var item in Model)
{
    @Html.Partial("_News", item);
}

See: Html.Partial not rendering partial view

Community
  • 1
  • 1
codemonkeh
  • 2,054
  • 1
  • 20
  • 36
  • That has fixed the exception thanks, but nothing is appearing in the
    where the partial should be rendered. What could be causing that?
    – user9993 Jun 03 '15 at 00:22
  • Everything looks fine to me, are you sure the collection is populated? – codemonkeh Jun 03 '15 at 00:25
  • I'm sure it is, I have a news page that displays news posts, which works correctly, and I'm making this partial view to basically display a version of the news on a homepage, I have the same ActionResult for both that and the code in my post. Where should I look to find the problem? – user9993 Jun 03 '15 at 00:26
  • Yep I totally missed the missing @! – user9993 Jun 03 '15 at 00:33
0

Okay so my mistake, I've solved it, I was missing @ on the Html.Partial!

I was surprised that this didn't throw up either an exception or actually display "Html.Partial..." as actual text.

Anyone know why neither of these were the case? If it's not thrown an exception then why wasn't it displayed as plain text?

user9993
  • 5,833
  • 11
  • 56
  • 117