开发者

ASP MVC Ordering Records By Date in a View?

开发者 https://www.devze.com 2022-12-27 17:38 出处:网络
I am outputting a students logged problems by passing the student into the view and then accessing their problems like this:

I am outputting a students logged problems by passing the student into the view and then accessing their problems like this:

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

I was wandering how I could sort these items by the ProblemDateTime attribute? I notice when I put a . after Prob开发者_JS百科lems there is a OrderByDescending option by I am not entirely sure how to use it?

Any help would be appreciated.

Thanks,

Jon


I would tend to use a different ViewModel that consist of a Student and Problems as different properties

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

Then your Controller should be responsible for returning the Student and the correct Problems

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号