ASP MVC在视图中按日期排序记录?

时间:2010-04-20 11:13:36

标签: asp.net-mvc

我通过将学生传入视图然后访问他们的问题来输出学生记录的问题:

foreach (var item in Model.Student.Problems) {

我在徘徊如何通过ProblemDateTime属性对这些项目进行排序?我注意到我放了一个。在问题之后有一个OrderByDescending选项,我不完全确定如何使用它?

任何帮助都将不胜感激。

谢谢,

乔恩

1 个答案:

答案 0 :(得分:0)

我倾向于使用由Student和Problems作为不同属性组成的不同ViewModel

public class MyViewModel
{
    public Student Student { get; set; }
    public IList<Problem> Problems { get; set; }
}

然后你的Controller应该负责退回学生和正确的问题

public ActionResult MyAction(int studentID)
{
    var model = new MyViewModel();

    //below is just an example:
    //would be much better if you had a service/repository layer 
    //that you could call to return your information

    model.Student = _db.Students.FirstOrDefault(s => d.ID == studentID);
    model.Problems = _db.Problems.Where(p => p.StudentID == studentID)
        .OrderByDescenging(p => p.Date)
        .ToList();

    return View(model);
}