c# - Using a partial view -


i'm trying understand how make partial views. far have following 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 in controller have:

@model ienumerable<site.services.news.newsitem> - belong here?  ...other controller code here...  @foreach(var item in model) {     html.partial("_news", item); } 

but i'm getting "nullreferenceexception" when try run application. doing wrong?

edit per comments:

        public actionresult index()     {         newsreader newsreader = new newsreader();         var newsitems = newsreader.getnewsitems();           return view(newsitems);     } 

@model site.services.news.newsitem  <div class="bs-callout bs-callout-primary">      <h2>          @html.displayfor(model => model.title)      </h2>  </div> 

there syntax error in code, should read model.title, not item.title because referring the model model in lambda expression.

i.e. same: @html.displayfor(x => x.title)

edit:

you need put @ symbol before html.partial

@foreach(var item in model) {     @html.partial("_news", item); } 

see: html.partial not rendering partial view


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -