如何在页面加载时显示随机电影

时间:2014-08-04 09:18:54

标签: javascript asp.net-mvc html5 c#-4.0 razor

我想在页面加载时从AppDomain.BaseDirectory中显示随机电影。

  RandomVideo.cshtml 

  <video width="320" height="240" controls>
  <source src="MyMovie1.mp4" type="video/mp4"> */ MyMovie1 should not be hard coded*/
  </video>

1 个答案:

答案 0 :(得分:2)

我将如何做到这一点:

视图

@model VideoModel

<video width="320" height="240" controls>
    <source src="@Model.VideoSource" type="video/mp4">
</video>

模型

public class VideoModel
{
    public string VideoSource { get; set; }
}

控制器

public class VideoController : Controller
{
    public ActionResult PlayVideo()
    {
        var model = new VideoModel();

        var videos = Directory
            .GetFiles(AppDomain.CurrentDomain.BaseDirectory)
            .Where(f => f.EndsWith(".mp4"))
            .ToList();

        var randomVideo = videos[new Random().Next(0, videos.Count)];

        model.VideoSource = randomVideo;

        return View(model);
    }
}