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

@model Portal.ViewModel.MemberMessageViewModel

@foreach (var msg in Model.MessageList)

<div> @html.DisplayFor(modelItem => msg.CreatedOn) @html.DisplayFor(modelItem => msg.Subject) </div>

MessageViewModel.cs

public class MemberMessageViewModel : BaseViewModel { public string Subject { get; set; } public string CreatedOn { get; set}

   public List<MemberMessageViewModel> MessageList { get; set;}
}

Controller

public async Task<IActionResult> 

Homepage()
{
  string MESSAGE_PANEL_NAME = _configuration.GetSection(“TemplateStrings”).GetSection(“TEMPLATE_MESSAGECENTER”).Value;

try{
 memberHomepageVM.messageDetail = await _messageService.GetHomepageMessages(MESSAGE_PANEL_NAME, Lang);
}

Cshtml

@awaitHtml.PartialAsync(“_message”, Model.messageDetail)

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:

       public class MessageService : IMessageService
         {   
            public List<MemberMessageViewModel> GetHomepageMessages()
             {
                 List<MemberMessageViewModel> messagelist = new List<MemberMessageViewModel>()
                 {
                     new MemberMessageViewModel(){ Subject="S1", CreatedOn=DateTime.Now.ToString()},
                     new MemberMessageViewModel(){ Subject="S2", CreatedOn=DateTime.Now.AddDays(-6).ToString()},
                     new MemberMessageViewModel(){ Subject="S3", CreatedOn=DateTime.Now.AddDays(-4).ToString()},
                     new MemberMessageViewModel(){ Subject="S4", CreatedOn=DateTime.Now.AddDays(-5).ToString()},
                     new MemberMessageViewModel(){ Subject="S5", CreatedOn=DateTime.Now.AddDays(2).ToString()},
                     new MemberMessageViewModel(){ Subject="S6", CreatedOn=DateTime.Now.AddDays(-8).ToString()},
                     new MemberMessageViewModel(){ Subject="S7", CreatedOn=DateTime.Now.AddDays(1).ToString()},
                 };
    
                 return messagelist;
             }
         }
    
  2. Register the service in the Startup.ConfigureServices method:

       services.AddScoped<IMessageService, MessageService>();
    
  3. Homepage controller:

     public async Task<IActionResult> Homepage()
     {
         //get the message list
         var messages = _messageService.GetHomepageMessages()
             .OrderByDescending(c => Convert.ToDateTime(c.CreatedOn))   //sort the message based on the CreatedOn property.
             .Take(5)  //take the top 5 messages.
             .ToList();
    
         //according to your view page bind model, it seems that you want to return a MemberMessageViewModel.
         MemberMessageViewModel member = new MemberMessageViewModel()
         {
             CreatedOn = DateTime.Now.ToString(),
             Subject = "Subject A",
             MessageList = messages,
         };
         //
         return View(member);
     }
    
  4. Homepage view page(Homepage.cshtml):

         @model Test.Models.MemberMessageViewModel
    
         @foreach (var msg in Model.MessageList)
         { 
             <div>
                 @Html.DisplayFor(modelItem => msg.CreatedOn)
                 @Html.DisplayFor(modelItem => msg.Subject)
             </div><br/>
         }
    

The output as below:

enter image description here

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

        @model Test.Models.MemberMessageViewModel

        @foreach (var msg in Model.MessageList.OrderByDescending(c => Convert.ToDateTime(c.CreatedOn)).Take(5))
        { 
            <div>
                @Html.DisplayFor(modelItem => msg.CreatedOn)
                @Html.DisplayFor(modelItem => msg.Subject)
            </div><br/>
        }

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

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