Viewdata排序在不同的列

时间:2017-10-13 12:48:52

标签: asp.net-mvc

我的控制器将存储的SQL过程中的数据保存到Viewdata

        using (var SPOutput = command.ExecuteReader())
        {
            foreach (var row in SPOutput)
            {
                outputmodel.Add(new SP_Procedure_RESULTS.student()
                {
                    student_id = (decimal)SPOutput4["student_id"],
                    age = (string)SPOutput4["age"],
                    course_id = (decimal)SPOutput4["course_id"],
                    building_no = (int)SPOutput4["building_no"],
                    room_id = (decimal)SPOutput4["room_id"],
                    status_id = (decimal)SPOutput4["status_id"],
                });

            }
            ViewData["Output"] = outputmodel;

但是我想在room_id(每行不同)的视图中显示它们。 我已经尝试将最后一行更改为

ViewData["Output"] = new SelectList(outputmodel.OrderBy(x => x.room_id).ToList());

但是,我在

视图中出现错误
System.NullReferenceException: 'Object reference not set to an instance of an object.'

@foreach (var item in ViewData["Output"] as IEnumerable<app.Models.SP_Procedure_RESULTS.student>)

有人可以告诉我如何更改viewdata的显示顺序吗?

感谢

1 个答案:

答案 0 :(得分:0)

对于寻找答案的其他人,以及Future me,控制器上的代码是: -

var order = outputmodel.OrderBy(c => c.room_no);
ViewData["Output"] = order;