razor - Problems with getting an image to show in a paged list -


i have following code, , displays listed text , pagination @ bottom fine. when try images child page list errors me. ideas why?

@inherits umbraco.web.mvc.umbracotemplatepage @{     layout = "sitelayout.cshtml"; }  <div role="main" id="main">       <div id="blogpagefeatures">         <!-- loop through of child pages (blog posts) , grab data output summary -->         @{         var pagesize =3;         var page =1;int.tryparse(request.querystring["page"],out page);         var items =model.content.children().where(x => x.isdocumenttype("blogarticle")).orderbydescending(x => x.createdate);         var totalpages =(int)math.ceiling((double)items.count()/(double)pagesize);          if(page > totalpages)         {                 page = totalpages;         }         else if(page <1)         {                 page =1;         }         }             @foreach(var item in items.skip((page -1)* pagesize).take(pagesize))         {                           <a href="@item.url">                                  <!-- next image line errors -->                                 <img src="@umbraco.media(item.children.blogarticleimage).url">                                      <h2>@item.name</h2>                                     <p class="blogtiledate">@item.createdate.tostring("dd/mm/yy")</p>                                  </a>          }   <div class="clearfix"></div>           <!-- pagination start -->         @if(totalpages >1)         {         <div class="pagination">                         <ul>                                 @if(page >1)                                 {                                         <li><a href="?page=@(page-1)">prev</a></li>                                 }                                 @for(int p =1; p < totalpages +1; p++)                                 {                                         var active =(p == page)?" class=\"active\"":string.empty;                                         <li@(html.raw(active))>                                                 <a href="?page=@p">@p</a>                                         </li>                                 }                                 @if(page <totalpages)                                 {                                         <li><a href="?page=@(page+1)">next</a></li>                                 }                         </ul>                 </div>                             }         <!-- pagination end -->              <div class="clearfix"></div>     </div>     <div class="clearfix"></div> </div> 

you getting model.content.children, typed collection of ipublishedcontent objects. calling item.children.blogarticlimage, not going work, you're trying property on children of item, not property of item itself. you're trying use dynamic syntax, won't work on typed objects. try changing image code to:

<img src="@umbraco.typedmedia(item.getpropertyvalue<int>("blogarticleimage")).url"> 

that should te trick, 1 thing note if item doesn't have image set, or image has been selected has been deleted, it'll throw error. more robust approach this:

var imagesrc = "add default image path here"; var media = umbraco.typedmedia(item.getpropertyvalue<int>("blogarticleimage")); if (media != null && !string.isnullorempty(media.url)) {     imagesrc = media.url; } <mig src="@imagesrc"> 

Comments