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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:23:42+00:00 2026-06-17T17:23:42+00:00

I like to push my view model structures out to my View and then

  • 0

I like to push my view model structures out to my View and then use those in javascript. What is a more elegant way to do this? These lines of code are kind of ugly.

<script type="text/javascript">
       var PartChangeModel = @Html.Raw(Json.Encode(Model.PartChange));
       var PartModel = @Html.Raw(Json.Encode(Model.Part));
       var BOMItemModel = @Html.Raw(Json.Encode(Model.BOMItem));
</script>
  • 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-17T17:23:43+00:00Added an answer on June 17, 2026 at 5:23 pm

    Per suggestion from A-Dubb, I put this into an extension method. Which way makes more sense? Probably 6 and half dozen kind of thing.

     public static MvcHtmlString ToJson(this Object ObjectToSerialize, string JavascriptVariableName)
            {
                return MvcHtmlString.Create(String.Format("var {0} = {1};", JavascriptVariableName, Json.Encode(ObjectToSerialize)));
            }
    

    Then you can call it from Razor like this:

    <script type="text/javascript">
        @Model.PartChange.ToJson("PartChangeModel")
        @Model.Part.ToJson("PartModel")
        @Model.BOMItem.ToJson("BOMItemModel")
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array in Knockout view model which looks like so: this.Activities =
I've got a knockout View Model that looks like this: var TagViewModel = function
I have a Knockout.js view model that looks like this: UserName:null, FirstName:null, LastName:null, Countries:{
I'm trying to push a view controller like so: SampleViewControllerB *svcb = [self.storyboard instantiateViewControllerWithIdentifier:@viewB];
I'm using a google events tracker like this: _gaq.push(['_setAccount', 'UA-1422398-23']); _gaq.push(['_trackEvent', 'BookingRequest', 'Parent Name',
My Core Data model looks like this: alt text http://fwdr.org/lg4q The players relationship of
I am using Mercurial version control for my project. Everything like push , pull
I'd like to push all users sending a request to a particular subdirectory back
We are developing a Customer feedback application and I would like to push the
is there a .net based comet engine? like Ajax Push Engine that is free

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.