I have a controller like this:
public ActionResult Index()
{
ViewBag.Title = "Index";
ViewBag.LoggedIn = TheUser.CheckStatus();
return View();
}
Thing is, I have to set LoggedIn to the output of my other function TheUser.CheckStatus() so that I can reference it with razor… Is there a way in Razor to access a function straight off? for example…
@TheUser.CheckStatus
instead of
@ViewBag.LoggedIn
The recommended way in MVC for passing information to a view is to create a model specific to that view (aka view model) e.g.
However, to answer your question:
If you are using ASP.NET Membership you can use the IsAuthenticated property on the request e.g.
Otherwise, you do need to pass this information to the view (whether that be via
ViewBag/view model etc.)Alternatively, you could write your own extension method for
Requestwhich would allow you to access it directly in the view:Or even as a
HtmlHelpere.g.Then in your views you can use
@Html.UserIsLoggedIn()which I think is what you are after.