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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:25:51+00:00 2026-06-15T19:25:51+00:00

In my MVC views I frequently need to reference various elements from JavaScript, and

  • 0

In my MVC views I frequently need to reference various elements from JavaScript, and for that purpose I define the id attribute for those elements. This is often in conjunction with Ajax calls that update content of various container elements.

A short example (Razor):

<table id="PersonList">
    ...
</table>
<div id="PersonDetails">
    <!-- This would be loaded dynamically using Ajax -->
    <input type="hidden" name="CurrentPersonId" value="@Model.PersonId">
    ...
</div>

This contains three “magic strings”: “PersonList”, “PersonDetails” and “CurrentPersonId”. An Ajax call might look like this:

$('#PersonDetails').load('@Url.Action("GetPersonDetails")', { PersonId: ... });

Again, the magic string “PersonDetails” appears.

Not good!

Can anyone suggest some “best practice” to define these magics string in a single place and use them in 1) the views, 2) preferably static JavaScript files that implement Ajax calls etc, and 3) CSS files?

I’m, thinking perhaps _Layout.cshtml could include an partial view that defines the magic strings for that controller or even for a specific action. It would examine what controller and/or action called it, and call the appropriate partial view based on that. I do something similar for .css and static .js already, letting me simply add Person.css and have that automatically included for all views for the Person controller.

The partial view would do something like this:

@{
    const string PersonListId = "PersonList";
    const string PersonDetailsId = "PersonDetails";
    const string CurrentPersonIdName = "CurrentPersonId";
}
<script type="text/javascript">
    NamesAndIds = {};
    NamesAndIds.PersonListId = '@Html.Raw(PersonListId)';
    NamesAndIds.PersonDetailsId = '@Html.Raw(PersonDetailsId)';
    NamesAndIds.CurrentPersonIdName = '@Html.Raw(CurrentPersonIdName)';
</script>

This should let Razor code use the C# string consts to generate appropriate HTML, and static JavaScript files could reference NamesAndIds in jQuery selectors etc. (Assumes that the consts defined in the partial view will be available in the calling view, which I doubt (haven’t checked it yet)… How to use them in .css files I don’t know.

Any better suggestions? How do you handle 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-06-15T19:25:52+00:00Added an answer on June 15, 2026 at 7:25 pm

    I hope someone can come up with something better, but this is at least something.

    In the main (non-partial) view I have a section at the top that defines the ids and names I need to use in multiple places in C#, HTML and JavaScript:

    @{
        const string PersonListId = "PersonList";
        const string PersonDetailsId = "PersonDetails";
        const string CurrentPersonIdName = "CurrentPersonId";
    }
    

    At the bottom, I have a script section that assigns the strings to suitable namespace container objects:

    <script type="text/javascript">
        MyNamespace = {};
        MyNamespace.Ids = {};
        MyNamespace.Names = {};
        MyNamespace.Ids.PersonList = '@Html.Raw(PersonListId)';
        MyNamespace.Ids.PersonDetails = '@Html.Raw(PersonDetailsId)';
        MyNamespace.Names.CurrentPersonId = '@Html.Raw(CurrentPersonIdName)';
    </script>
    

    In each partial view that introduces additional items that I need to reference by id or name, I add similar code to extend MyNamespace.Ids and MyNamespace.Names with the required strings.

    Now I can use the C# string constants in Razor view code to generate markup with the right ids and names and I can write regular static JavaScript files that reference MyNamespace.Ids and MyNamespace.Names to find the right ids and names, e.g. in jQuery selectors.

    I also added similar stuff for action URLs that my Ajax calls use, and put them in MyNamespace.Urls:

    MyNamespace.Urls.GetPerson = '@Html.Raw(Url.Action("GetPerson"))';
    

    It’s not ideal but it’s straightforward and solves the most pressing issue of magic strings scattered all over the place. It will not detect errors at compile time, but renaming items will require a single string to be renamed at a single place, and if I rename or misspell MyNamespace.Ids.Something it will at least generate a runtime JavaScript error that can be seen in a JS console or similar.

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

Sidebar

Related Questions

I updated my MVC Views from Webforms to Razor. What I did is that
Is there a lint utility for ASP.NET MVC? Given that I frequently specify views
I have 5 views in my ASP.NET MVC application. Some data were captured from
I insert into asp.net mvc views C# logic that manages layout like the following:
Can we Get those Values which is in Session directly in asp.net MVC Views?
I have various simple ASP.NET MVC views for CRUD operations which work fine on
i have this code in one of my asp.net mvc views: <%Html.RenderFile(@C:\Members\newsletters\welcome.html);%> I have
I prefer strongly typed viewdata for my asp.net mvc views for various reasons and
Is there a utility function for escaping JavaScript in ASP.NET MVC views? I often
I have a page composed from multiple partial asp.net mvc views rendered dynamically. How

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.