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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:45:02+00:00 2026-05-20T04:45:02+00:00

I think this is a common scenario – I have a view where I

  • 0

I think this is a common scenario – I have a view where I use HtmlHelper to generate some HTML elements, I also have a helper extension that lets me get the generated element’s ID, so that I can use it in JavaScript (e.g., jQuery):

$('#@Html.FieldIdFor(model => model.Name)').autocomplete({

Or when doing Ajax I’m building the URL string from the UrlHelper, again using server side code to put some client side stuff on the page:

$.get('@Url.Action("States", "Location")', { country: $(this).val() }, function (json) {

That part is easy. I know how to do this and I know I can put this code in a partial and render the partial view where I want the code to show up. This is not what I want to ask about.

Code contained within the page markup is not cached, that’s one thing. The other thing is that sometimes I need the same bit of code on several views and I would like to keep it in a single place for maintenance. Single place could be a partial view – but I want this code to be cached, ideally it would land in a .js file. However we cannot use server side code in .js files. The keywords are cacheable and single file.

I also know that I can have a JSController which would serve the JavaScript, for example:

<script src="@Url.Action("Script", "JS", { script = "location" })
        type="text/javascript"></script>

This controller action could return JavaScript as a result of a view rendering.

Or maybe I should stop being paranoid and just use normal .js files and put the element IDs and URLs there and if I ever update my view models or views, I’d go and update the .js files. I wonder whether this is an over engineering issue with .NET – I’d be interested to know how people do this in Rails or django.

So what I am really looking for are some “best practice” strategies. What do you do most often? How do you tackle with this problem?

  • 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-20T04:45:02+00:00Added an answer on May 20, 2026 at 4:45 am

    Caveat: I don’t do much with ASP.Net. But the problem isn’t ASP.Net-specific, it’s the same sort of thing for all applications that have both server- and client-side code.

    I’d probably have the main JavaScript file use IDs and such it gets from a variable, and have your per-view / per-page code generate the variables for it. So for instance:

    <script type='text/javascript'>
        if (!window.Stuff) {
            window.Stuff = {};
        }
        window.Stuff.MODEL_FIELD_SELECTOR = '#@Html.FieldIdFor(model => model.Name)';
    </script>
    <script type='text/javascript' src='commonstuff.js'></script>
    

    In your commonstuff.js:

    $(Stuff.MODEL_FIELD_SELECTOR).autocomplete({...
    // (Or `window.Stuff.MODEL_FIELD_SELECTOR`, but unless you redefine `Stuff`
    // locally, there's no need to prefix it.)
    

    Note that I’m using only one global symbol, Stuff, to contain this stuff, so you don’t pollute the global namespace more than necessary.

    The idea here is to put the code in a cachable, central, reusable place; but and to just use dynamically-generated code to set up the IDs of the things you need to work with.


    Update: If you’ll have several (or even a lot) of these, and I expect you probably will, and you’re using a symbol you know you’re the one defining, you can use object literal notation to make things more compact:

    <script type='text/javascript'>
        window.Stuff = {
            MODEL_FIELD_SELECTOR:  '#@Html.FieldIdFor(model => model.Name)',
            SOME_OTHER_SELECTOR:   '#@SomeOtherThing',
            SOMETHING_ELSE:        '#@SomethingElse',
            // ...
            LAST_ONE:              '#@TheLastOne'
            //                                   ^
            // Important: No trailing comma here |
        };
    </script>
    <script type='text/javascript' src='commonstuff.js'></script>
    

    Note that point about the last one; IE7 and earlier choke on trailing commas at the ends of object literals (more).

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

Sidebar

Related Questions

No related questions found

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.