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

  • Home
  • SEARCH
  • 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 6544901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:31:18+00:00 2026-05-25T11:31:18+00:00

I’m pretty new to MVC3 and I’ve started work on a newish jQuery /

  • 0

I’m pretty new to MVC3 and I’ve started work on a newish jQuery / MVC3 / EF 4.1 code first project and have been going over some of the existing code. One part of the code retrieves values from the db via a Controller action when ever the value of a certain drop down changes (SubtypeID.change event). Here’s the jQuery:

    $.getJSON(
    "/StudyDesign/GetSubtypes?typeId=" + typeId,
    function (data) {
        var theDropDown = document.getElementById("SubtypeID");

        if (data.length === 0) {
            theDropDown.disabled = true;
        }

        $.each(data, function () {
            $("#SubtypeID").append($('<option>').attr('value', this.SubtypeID).text(this.Name));
        });
    }
);

And here’s the Controller Action:

    [OutputCache(Duration = int.MaxValue, VaryByParam = "typeId", Location = OutputCacheLocation.Server)]
    public JsonResult GetSubtypes(int typeId)
    {
        var studyType = this._studyDesignRepository.StudyTypes.SingleOrDefault(s => s.StudyTypeID == typeId);
        return studyType == null ? this.Json(new List<Subtype>()) : this.Json(studyType.Subtypes.Select(s => new { s.SubtypeID, s.Name }).ToList(), JsonRequestBehavior.AllowGet);
    }

This works fine, but in our Controller constructor we have a lot of code that retrieves different values from the db that populate other dropdowns and grids etc. Once the page is loaded the first time we don’t need to to get these values again as they are already in the page, but every time the .change event is fired and StudyDesign/GetSubtypes is called, the Controller constructor runs and does all the db calls again. This seems unnecessary so I’m wondering

  • is there a better way to do this?
  • can/should we retrieve the json data a different way?
  • should we have the json Actions / methods in a separate Controller?

Thanks in advance 🙂

  • 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-25T11:31:19+00:00Added an answer on May 25, 2026 at 11:31 am

    In general, it’s a really bad idea to do a bunch of initialization like that in your controllers constructor. For starters, there’s no guarantee that this data will be there from call to call because the stateless nature of the app may cause the controller to get destroyed between calls. This means doing all your expensive work up front all the time.

    You should do that expensive work in your Action method, with the cacheing you’re using, those methods won’t get called again until the cache times out.

    Upon re-reading, it sounds like you’re talking about other data that gets populated on the page, but never gets changed because you’re doing Ajax calls. In that case, my point still stands. Do it in your action method for the page, rather than the constructor. then it will only load that data when you actually retrieve a page.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into

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.