2

I have assigned a list to ViewBag as below.

enter image description here

this is the code ...

var cer = tac.getAwards(hotelLocID);
            List<Certificates> allcertificates = new List<Certificates>();
            foreach (var items in cer)
            {
                allcertificates.Add(new Certificates
                {
                    certifyimageurllarge = items.awardimage.largeimage,
                    certifyimageurlsmall = items.awardimage.smallimage,
                    certifyimageurltiny = items.awardimage.tinyimage,
                    awardedyear = items.year,
                    awardtype = items.awardtype
                });
            }


            ViewBag.certificates = allcertificates;

in here allcertificates list count is 2.but I want to show only first one in the View.this is my view

@foreach(var items in @ViewBag.certificates)
                    {
                    <div class="row" style="padding-left:3%;">
                        <div class="col-md-2 col-xs-2"><img class="img img-responsive" src="@items.certifyimageurltiny" /></div><div class="col-md-6 col-xs-8" style="margin-left:-6%;padding-top:2%;">@items.awardtype | @items.awardedyear</div>
                    </div>
                    }

How can I do that.

Denis Bubnov
  • 2,619
  • 5
  • 30
  • 54
bill
  • 854
  • 3
  • 17
  • 41

3 Answers3

1

Try this instead of foreach:

<div class="row" style="padding-left:3%;">
    <div class="col-md-2 col-xs-2">
       <img class="img img-responsive" src="@ViewBag.certificates[0].certifyimageurltiny"/>
    </div>
    <div class="col-md-6 col-xs-8" style="margin-left:-6%;padding-top:2%;">
       @ViewBag.certificates[0].awardtype | @ViewBag.certificates[0].awardedyear
    </div>
</div>
Denis Bubnov
  • 2,619
  • 5
  • 30
  • 54
0

A minimal change would be to simply add a line with break; just above the closing }.

Peter B
  • 22,460
  • 5
  • 32
  • 69
0

job done in View side like this

 <div class="row" style="padding-left:3%;">
                        <div class="col-md-2 col-xs-2"><img class="img img-responsive" src="@ViewBag.certificates[0].certifyimageurltiny" /></div><div class="col-md-6 col-xs-8" style="margin-left:-6%;padding-top:2%;">@ViewBag.certificates[0].awardtype | @ViewBag.certificates[0].awardedyear</div>
                    </div>

this link : here helped me lot.thanx everyone who supprted me.

Community
  • 1
  • 1
bill
  • 854
  • 3
  • 17
  • 41