Reputation: 605
I have an MVC5 app, and in the HomeController, I have an ActionResult defined like this:
public ActionResult BlogRSS()
{
var model = new BlogModel();
string strFeed = "http://webmysite.com/feed";
using (XmlReader reader = XmlReader.Create(strFeed))
{
SyndicationFeed rssData = SyndicationFeed.Load(reader);
model.BlogFeed = rssData;
}
return View(model);
}
Then, for this ActionResult, I created a partial view named BlogRSS
, which looks like this:
@model MyApp.Models.BlogModel
@{
if (Model.BlogFeed != null)
{
<ul>
@foreach (var post in Model.BlogFeed.Items.ToList().Take(3))
{
<li><a href='@post.Links.First().Uri' target='_blank'>@post.Title.Text</a></li>
}
</ul>
}
}
And my model is defined simply like this:
public class BlogModel
{
public SyndicationFeed BlogFeed { get; set; }
}
So, the point is that I want to call that partial view in my _Layout.cshtml file, but when the website opens I get the error message specified in the title. I guess it is not calling my BlogRSS
method at all. I'm calling it in the _Layout.cshtml like this:
<div class="col-md-4">
Blog
<br />
@Html.Partial("BlogRSS")
</div>
How can I solve the problem, and make sure that the corresponding ActionResult is also called before rendering the View?
Upvotes: 0
Views: 887
Reputation: 12334
The problem is that you're putting a call to a partial view which just renders the view without calling the controller and the model passed to that view is null
.
There are couple ways how to fix this:
1) use Action
instead of Partial
@Html.Action("BlogRSS", "Blog")
2) Define a base ViewModel which you will pass to the each view and put your feed into it.
Upvotes: 0