-2

有没有办法从模型中随机获取值?

public string Oku()
        {

            var query = (from b in db.banner orderby Guid.NewGuid() select new { b.banner_image_name }).Take(1);



            return ("<img src='/banner_image/" + query.FirstOrDefault().banner_image_name.ToString() + "' width='154px' height='108px'/>");


        }
4

1 回答 1

1

获取项目,在 0 和横幅总数之间创建一个随机数,从横幅列表中获取该横幅

public string SomeImage()
{    
   var banners=db.banner.ToList();
   int totalBanners=banners.Count();
   Random rand=new Random();
   int randomNumb=rand.Next(0, totalBanners);
   var randomBanner=banners[randomNumb];
   return ("<img src='/banner_image/"+randomBanner..banner_image_name+"' />");
}
于 2012-08-15T13:29:50.207 回答