how to insert partial view from another controller in asp.net mvc 5 -


i want insert partial view newscontroller homecontroller.
in newscontroller

public actionresult lastnewspatial()         {             var lstlastnews = db.articles.take(5).orderbydescending(m => m.createddate).tolist();             return partialview(lstlastnews);         } 

in views/news folder create lastnewspatial.cshtml

@model ienumerable<mytest.com.models.article> @foreach (var item in model) {     <div class="glidecontent">         <img src="@item.imageurl" style="float: left; margin-right:21px;" />         <a href="#" class="title"><strong>@item.title</strong></a><br /><br />         @item.content     </div> } 

in views/home/index.cshtml insert lastnewspatial view

@html.partial("~/views/news/lastnewspatial.cshtml") 

when run project received error

object reference not set instance of object. 

at row

@foreach (var item in model)

in lastnewspatial.cshtml

how can fix it?

the reason error model isn't passed view... infact action isn't called @ all. set breakpoint , you'll confirm this.

i think should calling @html.renderaction inside index instead of @html.partial.

instead of @html.partial("~/views/news/lastnewspatial.cshtml")

use

@html.renderaction("lastnewspatial","news") 

or @html.action("lastnewspatial","news")


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -