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?
- create partial view contain unread messages etc
- create model pass view
- create "partial action"
- from _layout, call
html.renderaction
(orhtml.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
Post a Comment