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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:21:53+00:00 2026-05-21T02:21:53+00:00

I am still in the process of learning the ASP.NET MVC3 Razor engine, and

  • 0

I am still in the process of learning the ASP.NET MVC3 Razor engine, and I’m trying to wrap my head around how to accomplish something that seems so incredibly simple.

I have a static class that encapsulates a method which calls a third party API. My _Layout.cshtml master page calls the static class, e.g.

if(@Rf.Models.PageContent.GetSomething().IsSomethingHappening) {
   <span>@Rf.Models.PageContent.GetSomething().Name</span>
}

As you can see I am duplicating effort (and a call to the third party API) from my static class PageContent, method GetSomething — which returns a class.

In regular old MVC2, I would have done something like this:

<% var obj = Rf.Models.PageContent.GetSomething();
   if(obj.IsSomethingHappening) { %>
   <span><%=obj.Name%></span>
<% } %>

…which doesn’t duplicate effort.

I want to call PageContent.GetSomething() once, store it, and reference it if my flag is true. I do not want to call PageContent.GetSomething() again, if my flag is true.

This may be a very simple question, but I’m a bit stuck on how to accomplish it. How can I duplicate the same kind of functionality from within the Razor engine?

  • 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-05-21T02:21:54+00:00Added an answer on May 21, 2026 at 2:21 am

    The equivalent in Razor would be:

    @{
        var obj = Rf.Models.PageContent.GetSomething();
    }
    
    @if (obj.IsSomethingHappening)
    {
        <span>@obj.Name</span>
    }
    

    Obviously having views to pull data from static methods is one of the ugliest and anti-MVC practice. Views shouldn’t pull any data. They should only use data that was provided to them by the controller.

    So here’s the correct way to do this:

    public class FooController: Controller
    {
        public ActionResult Index()
        {
            var obj = Rf.Models.PageContent.GetSomething();
            return PartialView(obj);
        }
    }
    

    and in the corresponding view (~/Views/Foo/Index.cshtml):

    @model AppName.Models.SomeViewModel
    @if (Model.IsSomethingHappening)
    {
        <span>@Model.Name</span>
    }
    

    and to call this from your _Layout simply:

    @Html.Action("Index", "Foo")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am still in the process of learning Asp.Net. I have a question .
What happen if web.config gets updated while ASP (ASP.NET 2.0) server still process client
Im in the process of learning regular expressions but still cant really wrap my
I'm currently in the process of learning C++, and because I'm still learning, I
I'm currently in the process of learning ASP MVC and am running into a
sorry for the silly question, but I'm still in the process of learning Objective-C
I'm in the process of learning R, to wave SAS goodbye, I'm still new
Hi I'm sort new to wxPython and still in the process of learning. I'm
I'm still in the process of learning JavaScript. and I would like to complete
I'm still in the process of learning how to make android apps. I'm currently

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.