2017-07-04 17 views
0

日付(SermonDate)でテーブル(列、ID、名前、SermonDate、SermonTitle、BibleReading、VideoLink、BulletinLink)をソートしようとしています。私のMVCアプリケーションでそれを表示します。ASP.NET MVC - 日付順

いくつかの調査の後、私はthisthisを参照して以下を書こうとしましたが、何も私のために働いていません - おそらく私は間違ったコードを間違って入れてしまったからです。

public async Task<IActionResult> Index() 
{ 
    return View(await _context.Sermon.OrderBy(sermon => 
    DateTime.Now).Take(5).ToListAsync()); 
} 

私の現在のコントローラ(SermonsController.cs):

public SermonsController(BKPCContext context) 
    { 
     _context = context;  
    } 

    public async Task<IActionResult> Index() 
    { 
     return View(await _context.Sermon.OrderBy(sermon => DateTime.Now).Take(5).ToListAsync()); 
    } 

    public async Task<IActionResult> Details(int? id) 
    { 
     if (id == null) 
     { 
      return NotFound(); 
     } 

     var sermon = await _context.Sermon 
      .SingleOrDefaultAsync(m => m.ID == id); 
     if (sermon == null) 
     { 
      return NotFound(); 
     } 

     return View(sermon); 
    } 

    public IActionResult Create() 
    { 
     return View(); 
    } 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public async Task<IActionResult> Create([Bind("ID,Name,SermonDate,SermonTitle,BibleReading,VideoLink,BulletinLink")] Sermon sermon) 
    { 
     if (ModelState.IsValid) 
     { 
      _context.Add(sermon); 
      await _context.SaveChangesAsync(); 
      return RedirectToAction("Index"); 
     } 
     return View(sermon); 
    } 

、以下のように/Sermons/Index.cshtmlでHTMLテーブル:

<table class="table"> 
<thead> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.Name) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.SermonDate) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.SermonTitle) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.BibleReading) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.VideoLink) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.BulletinLink) 
     </th> 
     <th> 

     </th> 
    </tr> 
</thead> 
<tbody> 
@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.Name) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.SermonDate) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.SermonTitle) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.BibleReading) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.VideoLink) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.BulletinLink) 
     </td> 
     <td id="aspButton"> 
      <a role="button" id="btn-primary" class="btn btn-primary" asp-action="Edit" asp-route-id="@item.ID">Edit</a> 
      <a role="button" id="btn-danger" class="btn btn-danger" asp-action="Delete" asp-route-id="@item.ID">Delete</a> 
     </td> 
    </tr> 
    } 
</tbody> 

すべてのヘルプ本当に感謝します。

答えて

1
public class Sermon{ 
    public int ID {get;set;} 
    public DateTime DateAdded {get; set} 
} 

mySermonList.OrderByDescending(x => x.DateAdded); 
+0

私は既にSermonクラスを持っていましたので、 'mySermonList.OrderByDescending(x => x .DateAdded); 'このような私のコントローラーに:' return View(_context.Sermon.OrderByDescending(x => x.SermonDate).ToListAsync());ありがとう! – terryeah

+0

あなたはようこそです:) –

0

私はあなたがDateTime.Nowでソートされている問題 説教=> DateTime.Now

と思いますか? 説教は日付=>例

+0

ためdateaddedフィールド 説教を追加した場合、下部に最新の日付(最古の日付を私は現在、データのわずか3行(3つの異なる日付)を有するが、テーブルが降順でそれらを示しています上)。私はそれを逆にしたいし、最新の日付が上に来るようにしたい。 – terryeah

+0

あなたの説教オブジェクトはどのように見えますか?私はホープミステリーが正しいと思います、あなたはDateTime.Nowで注文しようとしています。実際の日付のプロパティ(?)はSermonオブジェクトのものではありません。 –