Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使值在控制器MVC3中持久化_C#_Asp.net Mvc 3 - Fatal编程技术网

C# 如何使值在控制器MVC3中持久化

C# 如何使值在控制器MVC3中持久化,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我在控制器中有以下代码: public ActionResult Index(string date) { var topsong = db.TopSongs; var topDate = db.TopDates; var latestDate = topDate.OrderByDescending(d => d.Date).FirstOrDefault(); if (dat

我在控制器中有以下代码:

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

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


           if (date == "PreviousDate")
            {
                latestDate = topDate.Where(d => d.Date < latestDate.Date).OrderByDescending(d => d.Date).FirstOrDefault();                
            }

            if (date == "NextDate")
            {
                latestDate = topDate.Where(d => d.Date > latestDate.Date).OrderBy(d => d.Date).FirstOrDefault();
            }           
.... 
return View();

}
    public ActionResult Index(string date, DateTime? latestDate)
    {
        var topsong = db.TopSongs;
        var topDate = db.TopDates;

        if (!latestDate.HasValue)
            latestDate = (DateTime?)(topDate.OrderByDescending(d => d.Date).FirstOrDefault());


       if (date.Value == "PreviousDate")
        {
            latestDate = (DateTime?)(topDate.Where(d => d.Date < latestDate.Date).OrderByDescending(d => d.Date).FirstOrDefault());                
        }

        if (date.Value == "NextDate")
        {
            latestDate = (DateTime?)(topDate.Where(d => d.Date > latestDate.Date).OrderBy(d => d.Date).FirstOrDefault());
        }

        .... 

        ViewBag.latestDate = latestDate

        return View();
    }

您可能想做:

return View(latestDate);

因此,模型绑定器将其拾取

您可能需要执行以下操作:

return View(latestDate);

因此,模型绑定器将其拾取

您将希望将当前显示的日期以及上一个日期和下一个日期的“子动作”传递回控制器

在控制器中:

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

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


           if (date == "PreviousDate")
            {
                latestDate = topDate.Where(d => d.Date < latestDate.Date).OrderByDescending(d => d.Date).FirstOrDefault();                
            }

            if (date == "NextDate")
            {
                latestDate = topDate.Where(d => d.Date > latestDate.Date).OrderBy(d => d.Date).FirstOrDefault();
            }           
.... 
return View();

}
    public ActionResult Index(string date, DateTime? latestDate)
    {
        var topsong = db.TopSongs;
        var topDate = db.TopDates;

        if (!latestDate.HasValue)
            latestDate = (DateTime?)(topDate.OrderByDescending(d => d.Date).FirstOrDefault());


       if (date.Value == "PreviousDate")
        {
            latestDate = (DateTime?)(topDate.Where(d => d.Date < latestDate.Date).OrderByDescending(d => d.Date).FirstOrDefault());                
        }

        if (date.Value == "NextDate")
        {
            latestDate = (DateTime?)(topDate.Where(d => d.Date > latestDate.Date).OrderBy(d => d.Date).FirstOrDefault());
        }

        .... 

        ViewBag.latestDate = latestDate

        return View();
    }
公共操作结果索引(字符串日期、日期时间?最晚日期) { var topsong=db.TopSongs; var topDate=db.TopDates; 如果(!latestDate.HasValue) latestDate=(DateTime?)(topDate.OrderByDescending(d=>d.Date.FirstOrDefault()); 如果(date.Value==“PreviousDate”) { latestDate=(DateTime?)(topDate.Where(d=>d.Dated.Date).FirstOrDefault()); } 如果(date.Value==“NextDate”) { latestDate=(DateTime?)(topDate.Where(d=>d.Date>latestDate.Date).OrderBy(d=>d.Date).FirstOrDefault()); } .... ViewBag.latestDate=latestDate 返回视图(); } 然后

@Html.ActionLink("Previous Week's Songs", "Index", new { date = "PreviousDate", ViewBag.latestDate }) <br />
@Html.ActionLink("Next Week's Songs", "Index", new { date = "NextDate", ViewBag.latestDate })
@Html.ActionLink(“上周的歌曲”,“索引”,新的{date=“PreviousDate”,ViewBag.latestDate})
@ActionLink(“下周的歌曲”,“索引”,new{date=“NextDate”,ViewBag.latestDate})
您需要将显示的当前日期以及上一个日期和下一个日期的“子动作”传递回控制器

在控制器中:

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

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


           if (date == "PreviousDate")
            {
                latestDate = topDate.Where(d => d.Date < latestDate.Date).OrderByDescending(d => d.Date).FirstOrDefault();                
            }

            if (date == "NextDate")
            {
                latestDate = topDate.Where(d => d.Date > latestDate.Date).OrderBy(d => d.Date).FirstOrDefault();
            }           
.... 
return View();

}
    public ActionResult Index(string date, DateTime? latestDate)
    {
        var topsong = db.TopSongs;
        var topDate = db.TopDates;

        if (!latestDate.HasValue)
            latestDate = (DateTime?)(topDate.OrderByDescending(d => d.Date).FirstOrDefault());


       if (date.Value == "PreviousDate")
        {
            latestDate = (DateTime?)(topDate.Where(d => d.Date < latestDate.Date).OrderByDescending(d => d.Date).FirstOrDefault());                
        }

        if (date.Value == "NextDate")
        {
            latestDate = (DateTime?)(topDate.Where(d => d.Date > latestDate.Date).OrderBy(d => d.Date).FirstOrDefault());
        }

        .... 

        ViewBag.latestDate = latestDate

        return View();
    }
公共操作结果索引(字符串日期、日期时间?最晚日期) { var topsong=db.TopSongs; var topDate=db.TopDates; 如果(!latestDate.HasValue) latestDate=(DateTime?)(topDate.OrderByDescending(d=>d.Date.FirstOrDefault()); 如果(date.Value==“PreviousDate”) { latestDate=(DateTime?)(topDate.Where(d=>d.Dated.Date).FirstOrDefault()); } 如果(date.Value==“NextDate”) { latestDate=(DateTime?)(topDate.Where(d=>d.Date>latestDate.Date).OrderBy(d=>d.Date).FirstOrDefault()); } .... ViewBag.latestDate=latestDate 返回视图(); } 然后

@Html.ActionLink("Previous Week's Songs", "Index", new { date = "PreviousDate", ViewBag.latestDate }) <br />
@Html.ActionLink("Next Week's Songs", "Index", new { date = "NextDate", ViewBag.latestDate })
@Html.ActionLink(“上周的歌曲”,“索引”,新的{date=“PreviousDate”,ViewBag.latestDate})
@ActionLink(“下周的歌曲”,“索引”,new{date=“NextDate”,ViewBag.latestDate})
我已经在使用返回视图传回Ienumerable返回视图(歌曲);我可以传回两个值吗?是的,创建一个具有所需两个属性的ViewModel。类似于SongDateViewModel->中有日期属性和任何类型的歌曲。让控制器返回ViewModel。ViewModel是一种传递更复杂类型的方法。我已经在使用return View传回Ienumerable return View(歌曲);我可以传回两个值吗?是的,创建一个具有所需两个属性的ViewModel。类似于SongDateViewModel->中有日期属性和任何类型的歌曲。让控制器返回ViewModel。ViewModel是一种可以传递更复杂类型的方法。或者,正如@kd7在上面所建议的,您可以使用(应该?)ViewModel而不是ViewBagI,就像您提供的上述解决方案一样,在这种情况下,我不需要太多的代码更改。但latestDate.Date无法使用上述解决方案。有什么想法吗?这是因为latestDate是一个可为空的DateTimes,直到现在仍在工作,但出现了一个错误:无法隐式地将Models.Top10.Date转换为System.DateTime?Top10.Date的类型是什么?也许它是一个时间戳。或者正如上面@kd7所建议的,您可以使用(应该?)ViewModel而不是ViewBagI,就像您提供的上述解决方案一样,在这种情况下,我不需要太多的代码更改。但latestDate.Date无法使用上述解决方案。有什么想法吗?这是因为latestDate是一个可为空的DateTimes,直到现在仍在工作,但出现了一个错误:无法隐式地将Models.Top10.Date转换为System.DateTime?Top10.Date的类型是什么?也许这是一个时间戳。