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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:11:29+00:00 2026-06-07T03:11:29+00:00

I want to know if it is a good practice to use razor in

  • 0

I want to know if it is a good practice to use razor in JavaScript code. For example:

<script type="text/javascript">
   var variable = @some.Id
</script>

Or it’s better to create hidden value and then take it with JavaScript, like this?

<input type="hidden" id="someId" value"@some.Id" />

<script type="text/javascript">
   var variable = $('#someId').val();
</script>

EDIT:

@{
var formVariables = serializer.Serialize(new
                                             {
                                                 id = Model.Id, 
                                                 name = Model.Name, 
                                                 age = Model.Age
                                             });

<input type="hidden" id="header_variables" value="@formVariables"/>
<script type="text/javascript" src = "/Scipts/..."></script>
}

Is this good solution?

  • 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-07T03:11:31+00:00Added an answer on June 7, 2026 at 3:11 am

    I personally would go with an extension of the 2nd option and create a seperate .js file. The reason being, if you delegate work out to a 3rd party to take care of the jquery/javascript parts of the UI, then they need not have any sight of the backend functionality.

    There are a variety of ways to use html5 attributes (i.e. data-attribute=’foo’) on the inputs which would allow you to ‘decorate’ your inputs with a cargo of properties which could be parsed inside the external .js file.

    A very brief example:

    in your view:

    <input type='text' id='myId' data-action='@Url.Action("MyAction")' class='myClass' />
    

    in your .js file:

    var targetAction = $('#myId').attr('data-action');
    

    this gives complete separation between the .js and the views. It does require a degree of planning of course.

    Hope this helps

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

Sidebar

Related Questions

Hello I would like to know if my script is good; I want to
I want to know what is good practice for loop a form option in
I want to know a good way of converting the Model to ViewModel and
i want to know how Delegate is differ from object in cocoa any good
i just want to know a general information about this particular information. Any good
Fixing memory leaks is good thing, but I want to know apart from memory
As an exercise in good OO methods and design, I want to know what
I don't understand garbage collection so good, then I want to know, why it's
I am working through a code sample, and I just want to hear some
I know it's good practice to design different menu icons for different screen densities.

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.