MVC3从模型中获取随机值

时间:2012-08-15 13:15:49

标签: asp.net-mvc-3

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

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'/>");


        }

1 个答案:

答案 0 :(得分: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+"' />");
}