Last post Oct 16, 2017 01:39 AM by Jean Sun
Oct 13, 2017 11:04 AM|G-Oker|LINK
I have my controller saving data from an stored SQL procedure into a Viewdata
using (var SPOutput = command.ExecuteReader())
foreach (var row in SPOutput)
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;
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.'
at the line
@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 ?
Oct 13, 2017 03:38 PM|G-Oker|LINK
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
Oct 16, 2017 01:39 AM|Jean Sun|LINK
Thanks for your sharing.