Skip to content
Advertisement

Access Messages from Message Center

Hi I’m try to display the top 5 messages from the message center on the homepage. But I’m not sure what I’m doing wrong. I try to get the messages from the message view model here is my code.

View model

JavaScript

MessageViewModel.cs

JavaScript

Controller

JavaScript

Cshtml

JavaScript

Advertisement

Answer

To display top 5 records, we should use the “OrderByDescending()” to sort the message, and then use the “Take(5)” method to get the top 5 messages.

Please refer the following sample code:

  1. Based on your MemberMessageViewModel create a service, and set initial data:

    JavaScript
  2. Register the service in the Startup.ConfigureServices method:

    JavaScript
  3. Homepage controller:

    JavaScript
  4. Homepage view page(Homepage.cshtml):

    JavaScript

The output as below:

enter image description here

Besides, you could also sort and take the messages from the View page:

JavaScript

If I misunderstand your problem, please let me know freely.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement