简体   繁体   中英

MVC Controller Query Result not displaying in View

I am trying to sort a database table and display the results in a View in MVC. The query in the Controller returns the sorted results in a console application (which references the same database) but the list displays as unsorted in the View. What am I doing wrong/ missing?

     public class HomeController : Controller 

{
    private SwimTimesEntities db = new SwimTimesEntities();

    public ActionResult Index()
    {
                var query = from s in db.Swims
                    orderby s.Day
                    select s; 

        return View(query.ToList());
    }

And the Model is

    public partial class Swim
{
    public int SwimID { get; set; }
    public System.TimeSpan Time { get; set; }

    public string Day { get; set; }
    public string Details { get; set; }
}

}

(Very) New to C# and MVC, pointers much appreciated.

In your View try using a Model , for sample:

@model List<Swim>

<ul>
@foreach(var item in Model)
{
   <li>@item.Day - @item.Detail</li>
}
</ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM