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 9177795
In Process

The Archive Base Latest Questions

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

I am using asp.net MVC 4, I’d like to do something like this in

  • 0

I am using asp.net MVC 4, I’d like to do something like this in my view

<a href=@NamespaceHere.StaticUtil.TheFunctionIUseAlot("/blah", 1, Viewbag, "link and view specific") class="thing">my link</a>

I don’t like it. Is there a way I can have the namespace implicit? Can I not pass in Viewbag and grab it somehow like global.request.current.viewbag?

If it helps I don’t need TheFunctionIUseAlot to be static. I just need it to be easily callable in all my views and hopefully i’d like to not pass in viewbag. The reason I pass viewbag is because i need something in the controller which i stuck in viewbag as well. Maybe i can put it in the model but i don’t want the same problem where i am passing model instead of viewbag

  • 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:24:29+00:00Added an answer on June 17, 2026 at 5:24 pm

    You can make the namespace implicit by adding @using NamespaceHere; at the top of your view, or by registering it as a namespace in the views web config. Something like this:

    <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="System.Web.Mvc.WebViewPage">
          <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
            <add namespace="NamespaceHere"/>
          </namespaces>
        </pages>
      </system.web.webPages.razor>
    

    You can get access to the viewbag in your function if you use a custom page base type for your views. You can find instructions on doing that here: http://haacked.com/archive/2011/02/21/changing-base-type-of-a-razor-view.aspx.

    In your situation, to get access to the ViewBag you will want to define your FunctionThatIUseALot in a non-static class that takes a WebViewPage as a constructor parameter (either as dynamic or as a ViewDataDictionary). Then, in your WebViewPage’s InitHelpers override, you can create your helper class, passing in this. That will give you access to all of the standard properties of the WebViewPage.

    Something like this:

    public class Utils {
        private WebViewPage page;
        public Utils(WebViewPage page) { this.page = page; }
    
        public FunctionThatIUseALot(string someParameter) {
            string someViewBagValue = page.ViewBag.SomeViewBagValue;
        }
    }
    
    public abstract class CustomWebViewPage : WebViewPage {
      public Utils Utils { get; set; } // Utils is your helper class
    
      public override void InitHelpers() {
        base.InitHelpers();
        Utils = new Utils(this);
      }
    }
    

    The above is not everything you need to do to make it work, you will need to read the article for the rest. But the end result should be that you can just call @Utils.FunctionThatIUseALot("some value") directly in your views.

    Note: you will need to do this twice – once for the standard WebViewPage base class, and one for the WebViewPage<TModel> base class.

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

Sidebar

Related Questions

Using ASP.NET MVC's default view engine, you can declare a server-side comment like this:
Using asp.net MVC I'd like to do this inside a view: <%= Html.TextBox(textbox1, null,
Using ASP.NET MVC & Spark, I have a view that is listing a number
Using ASP.NET MVC can be used the view engine Razor. Razor let you use
I am using ASP.NET MVC and jQuery. I have this code in my js
Using ASP.NET MVC 4 Web API public IEnumerable<Item> GetAll(GetItemRequest request) { This throws an
I am using Asp.net MVC 3 and Json.net library. I have view model class
Using asp.net mvc, a user updates his profile. This is how my update action
'm using ASP.NET MVC 3 with Razor views. I have a partial view which
I'm actually using ASP.Net MVC, but I think this applicable to ASP.Net as well.

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.