如何传回actionlink值 - mvc3

时间:2012-08-26 18:20:03

标签: asp.net-mvc-3 c#-4.0

我在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日期 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 });

基本上我只想使用动作链接转到上一个或下一个日期。我尝试了上面的代码,但它没有用。

2 个答案:

答案 0 :(得分:1)

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

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
}

答案 1 :(得分: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")

<强>更新

嘿,好像您没有任何特定的代码将您的模型数据绑定到View ...所以请尝试更新您的View代码,如下所示

    @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 });

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