1

我有以下代码HomeController;它获取最新日期并根据该日期显示歌曲。

public ActionResult Index()
{           
   var topsong = db.TopSongs;
   var topDate = db.TopDates;
   var latestDate = topDate.OrderByDescending(d => d.Date).FirstOrDefault();
   int dateId = topDate.OrderByDescending(d => d.DateId).FirstOrDefault().DateId;
   var topTitles = topsong.Where(s => s.DateId == latestDate.DateId).ToList();

   ViewBag.Message = "Top Songs";                                    
   ViewBag.latestDate = latestDate.Date.ToShortDateString();            

   return View(topTitles);
}

这会进入数据库并选择最新日期——比如 08/25/2012——并显示与该日期对应的所有歌曲。这部分工作正常;现在我想添加一个功能,用户可以从数据库中选择以前的日期或下一个日期,并据此显示歌曲。

我想要做的是在视图中添加上一个和下一个日期按钮。像这样:

<h2>Songs Updated On: @ViewBag.latestDate</h2>
@Html.ActionLink("Previous Week's Songs", "Index", new { });
@Html.ActionLink("Next Week's Songs", "Index", new { });

由于我是 MVC3 的新手,有人可以告诉我如何让上一个和下一个日期出现吗?如果我在视图中计算它们,我如何将它们传递回控制器以显示这些日期的歌曲?

这是示例日期表: DateId Date 1 08/25/2012 2 08/20/2012 3 09/21/2012

更新代码:

这是 HomeController 的代码:

 public ActionResult Index(DateTime? date = null)
        {            
            var topsong = db.TopSongs;
            var topDate = db.TopDates;

            var latestDate = (date.HasValue) ? date.Value : topDate.OrderByDescending(d => d.Date).FirstOrDefault();           


            //var latestDate = topDate.OrderByDescending(d => d.Date).FirstOrDefault();

            int dateId = topDate.OrderByDescending(d => d.DateId).FirstOrDefault().DateId;           
            var topTitles = topsong.Where(s => s.DateId == latestDate.DateId).ToList();

            /////
            ViewBag.NextDate = topDate.Where(d => d.Date > latestDate).OrderBy(d => d.Date).FirstOrDefault();
            ViewBag.PreviousDate = topDate.Where(d => d.Date < latestDate).OrderByDescending(d => d.Date).FirstOrDefault();
            ////////

            ViewBag.Dates = topDate.Select(d => d.Date).Distinct();
            ViewBag.latestDate = latestDate.Date.ToShortDateString();            

            return View(topTitles);
        }

Index.cshtml 的代码

@model IEnumerable<App.Models.TopSong>
@{
    ViewBag.Title = "Top Songs";
    var songs = ViewBag.songs;
}            
<h1>@ViewBag.Message</h1>

<h2>Song's Updated On: @ViewBag.latestDate</h2>
@Html.ActionLink("Previous Week's Songs", "Index", new { date=@ViewBag.PreviousDate });
@Html.ActionLink("Next Week's Songs", "Index", new { date=@ViewBag.NextDate });

基本上我只是希望能够使用操作链接转到上一个或下一个日期。我尝试了上面的代码,但它不起作用。

4

2 回答 2

1

对于第一个问题,答案是创建一个View Model。这里的想法是提供视图所需的所有数据。例如:

public class ViewModel
{
    public string CurrentDate { get; set; }
    public DateTime NextDate { get; set; }
    public DateTime PreviousDate { get; set; }
    public List<Song> Songs { get; set; }
}

因此,您将根据需要填充它,例如

var topDate = db.TopDates;
var model = new ViewModel();
model.Songs = ...
model.CurrentDate = latestDate.Date.ToShortDateString();
model.NextDate = topDate.Where(d => d.Date > latestDate).OrderBy(d => d.Date).FirstOrDefault(d => d.Date);
model.PreviousDate = topDate.Where(d => d.Date < latestDate).OrderByDescending(d => d.Date).FirstOrDefault(d => d.Date);

现在您可以使用模型中的值在 Razor 视图中创建链接:

@Html.ActionLink("Previous Week's Songs", "Index", new { date=Model.PreviousDate });
@Html.ActionLink("Next Week's Songs", "Index", new { date=Model.NextDate });

最后,要处理回发中的链接,请在 Action 方法中添加一个与上面指定的路由值相匹配的参数 ( date)。

public ActionResult Index(int? date = null)
{
    var latestDate = (date.HasValue) ? date.Value : db.TopDates.OrderByDescending(d => d.Date).FirstOrDefault(d => d.Date);
    // same logic
}
于 2012-08-27T00:11:16.600 回答
1

一种方法如下:

一、创建动作方法

Public ActionResult Songs(string val)
{
   if (val == "Previous")
   {
    // code to get and return previous week songs
   }

   if (val == "Next")
   {
    // code to get and return next week songs
   }

    // By default return either previous/next/current week's songs...
}

其次,修改您的操作链接以匹配如下

@Html.ActionLink("Previous Week's songs", "Songs", new { val = "Previous"})

@Html.ActionLink("Next Week's Songs", "Songs", new { val = "Next"})

另一种方法:

另一种方法是为每个方法创建一个操作方法,这意味着您可以有一个操作方法只返回上一周的歌曲,而另一个操作方法返回下周的歌曲。这意味着您不需要传递任何参数,并且您的 actionlink 代码将如下所示简单

   @Html.ActionLink("Previous Week's songs", "PreviousWeekSongs")

   @Html.ActionLink("Next Week's Songs", "NextWeekSongs")

更新:

嘿,您似乎没有任何特定于将模型数据绑定回视图的代码...因此请尝试如下更新您的视图代码

    @model IEnumerable<App.Models.TopSong>
@{
    ViewBag.Title = "Top Songs";
    var songs = ViewBag.songs;
}            
<h1>@ViewBag.Message</h1>
// Code to Display Songs
@foreach(var song in model)
{
  <div> 
   <span>@song.Property1</span>
   <span>@song.Property2</span>
   <span>@song.Property3</span>          
  </div>

}

<h2>Song's Updated On: @ViewBag.latestDate</h2>
@Html.ActionLink("Previous Week's Songs", "Index", new { date=@ViewBag.PreviousDate });
@Html.ActionLink("Next Week's Songs", "Index", new { date=@ViewBag.NextDate });

我希望这可以帮助你...

于 2012-08-27T15:48:00.197 回答