asp.net mvc - Ordering items from a model in a list C# -
i'm new mvc 4 , i'm trying figure out how order list being created reading in model.
here model code:
public class files { [key] public int file_id { get; set; } public string original_file_name { get; set; } public string current_file_name { get; set; } public string description { get; set; } public string file_path { get; set; } public string file_type { get; set; } public string file_status { get; set; } public datetime expiry_date { get; set; } //public int uploaded_by { get; set; } //public datetime uploaded_on { get; set; } } public class filescontext : dbcontext { public dbset<files> files { get; set; } }
here controller code creates list:
return view(db.files.tolist());
lastly html writes screen:
@foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.original_file_name) </td> <td> @html.displayfor(modelitem => item.current_file_name) </td> <td> @html.displayfor(modelitem => item.file_type) </td> <td> @html.displayfor(modelitem => item.file_status) </td> <td> @html.displayfor(modelitem => item.expiry_date) </td> <td> @html.actionlink("details", "details", new { id=item.file_id }) | <!--@html.actionlink("test", "test", new { id=item.file_id }) |--> @html.actionlink("delete", "delete", new { id=item.file_id }) </td> </tr> }
order file name ascending:
db.files.orderby(file => file.original_file_name).tolist();
order file name descening:
db.files.orderbydescending(file => file.original_file_name).tolist();
multiple order by:
db.files.orderby(file => file.original_file_name).thenby(file => file.expiry_date).tolist();
you can refer: msdn: linq sorting operations
Comments
Post a Comment