asp.net mvc 4 - MVC 4 Update Menu On Every View Load -


i have shared view _layout.cshtml contain number of unread messages user. need check new messages every time user visits new view. @ time, don't see need on timed ajax-type call.

i can day master pages on .net webforms i'm having hard time doing simple mvc.

my question is: how can trigger database call on every view load check , see if there change in number of messages , update text in _layout.cshtml?

  1. create partial view contain unread messages etc
  2. create model pass view
  3. create "partial action"
  4. from _layout, call html.renderaction (or html.action())

we prefix partials underscore.

views\shared\_unreadmessagespartial.cshtml

@model unreadmessagesviewmodel  unread messages: @model.unreadmessagescount 

unreadmessagesviewmodel.cs

public class unreadmessagesviewmodel {     private dbcontext _db;      public int unreadmessagescount;      public unreadmessagesviewmodel() {         _db = new dbcontext();         unreadmessages = _db.messages.count();     } } 

homecontroller.cs

public class homecontroller : controller {     // mark action unavailable general public     [childactiononly]     public actionresult _unreadmessagespartial()     {         unreadmessagesviewmodel viewmodel = new unreadmessagesviewmodel();         return partialview(viewmodel);     } } 

views\shared\_layout.cs

@{     html.renderaction("_unreadmessagespartial"); } 

i hope c# ok, had convert vb.net in head.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -