Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9179647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:51:01+00:00 2026-06-17T17:51:01+00:00

I have a controller like this: public ActionResult Index() { ViewBag.Title = Index; ViewBag.LoggedIn

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:51:03+00:00Added an answer on June 17, 2026 at 5:51 pm

    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.

    public class IndexViewModel
    {
        public string Title { get; set; }
        public bool IsAuthenticated { get; set; }
    }
    ....
    public ActionResult Index()
    {
        return View(new IndexViewModel()
        {
            Title = "Index",
            IsAuthenticated = UserIsLoggedIn()
        });
    }
    

    However, to answer your question:

    Is there a way in Razor to access a function straight off?

    If you are using ASP.NET Membership you can use the IsAuthenticated property on the request e.g.

     @Request.IsAuthenticated
    

    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 Request which would allow you to access it directly in the view:

    @Request.UserLoggedIn()
    

    Or even as a HtmlHelper e.g.

    public static class HtmlHelperExtensions
    { 
        public static bool UserIsLoggedIn(this HtmlHelper helper)
        {
            return /* authentication code here */
        }
    }
    

    Then in your views you can use @Html.UserIsLoggedIn() which I think is what you are after.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller like this: public FooController : Controller { public ActionResult Index()
I have a Controller that currently looks like this: public ActionResult Index() { var
New to mvc, I have a controller like this public ActionResult Index(DateTime Date, DateTime
I have this code in my controller for Index view.. public ActionResult Index(int? id)
I have a controller action like this: public ActionResult Index(string url) { var pageTitle
I have this Controller : public class ProfileController : Controller { public ActionResult Index(
I have an action method that looks like this: public ActionResult Index(string message) {
I have a controller that looks like this: public class PageController : Controller {
I have a Controller with syntax like this: public class CrudController<TEntity> : Controller Now
I have 2 functions/methods in the same controller in CodeIgniter like this: public function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.