ViewData在不同的列上排序 [英] Viewdata sort on different column

查看:68
本文介绍了ViewData在不同的列上排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have my controller saving data from an stored SQL procedure into a 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(每行不同)的顺序显示它们.我尝试将最后一行更改为

but I would like to display them in my view in room_id (which is different for each row) order. I have tried changing the last line to

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

但是,我随后在我的观点中得到了一个错误

but , I then get a error in my view of 

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>)

有人可以告诉我如何实现更改显示视图数据的顺序吗?

Could someone show me how I can achieve changing the order in which the viewdata is displayed ?

谢谢

推荐答案

对于其他任何寻求答案的人,以及对我来说,起作用的控制器上的代码是:-

For anyone else looking for an answer , and Future me, the code on the controller that works is :-

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

这篇关于ViewData在不同的列上排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆