I have a partial view called _News that when called by itself works as expected.
When I call it from another view using the following code:
<div>
@html.Partial("_News");
</div>
It throws this error:
Object reference not set to an instance of an object
At this line of code in the view:
@foreach (var item in Model) {
Where the view is referencing the model. I realise this means that the view is not being passed the model from the Controller but I am perplexed as to why.
The Controller is called NewsController and is located in Controllers. The View is called _News and is located in Shared Views. The view calling the partial view is the default home/index page.
If your partial needs to access data from the model, you need to pass the model into the
Partial()method:MSDN: http://msdn.microsoft.com/en-us/library/system.web.mvc.html.partialextensions.partial%28v=vs.108%29.aspx
EDIT:
Per your comment below, I think you’re actually after this: http://haacked.com/archive/2009/11/17/aspnetmvc2-render-action.aspx – this lets you call a controller action and render the result into the current view.