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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:14:29+00:00 2026-06-09T19:14:29+00:00

So I have a problem…I am separating concerns in my web app: HTML in

  • 0

So I have a problem…I am separating concerns in my web app: HTML in razor pages and JS in js files. My problem lies in the fact that I want to use data from my controller passed down in the view model from the server as the options of a select list. The problem lies in the fact that I have separated my js from my HTML and I can’t access Razor inside the js files.

I have a list of items coming down into my view model…


public List Stuffs { get; set; }

I json encode it server side and make sure to take care of circular refrences, so it looks like this

[{"id":1,"name":"blah"},{"id":2,"name":"blah2"},{"id":3,"name":"blah3"},{"id":4,"name":"blah4"}]

The problem is, I want to keep my separation of concerns, so how do I get that array into my js file so I can bind it to a select list using knockout? I definitely don’t want to do another round trip to the server.

  • 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-09T19:14:31+00:00Added an answer on June 9, 2026 at 7:14 pm

    Does your Javascript file contain a view model in the form of an instantiable function, e.g. something like:

    function MasterVM(data)
    

    or an object literal, e.g.

    var masterVM = { ...
    

    ? I tend to go for instantiable view models (partly because they chain down through a hierarchy of view models using the mapping plugin – the top level one builds its children) and that being the case I think it’s nice for the Razor view to instantiate the view model with the JSON rendered from the MVC view model, e.g.:

    var masterVM = new MasterVM(@(Html.Raw(JsonConvert.SerializeObject(Model.ProductViewModel))));
    

    Or even have the Knockout view model “owned” by a revealing module and initialise it like:

    productModule.init(@(Html.Raw(JsonConvert.SerializeObject(Model.ProductViewModel))));
    

    Then productModule is also responsible for other things like mediating between your view model(s) and the server, watching for dirty state, etc.

    Also, doing another round trip to the server is not necessarily bad if you are sourcing a massive set of reusable options which you’d like the browser to cache. If you are, you might want to have an MVC controller which emits a big object literal containing all of your commonly used options, which you can then use as the “options” property across multiple selects. But if your options are specific to the particular view model, then I guess they have to be part of your view model.

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

Sidebar

Related Questions

I have problem while loading data into html select when users press or click
I have problem developing with live555. I already build the lib-files and example projects
I have problem with php's preg_match.I want to make something like this : When
Have problem with linq expressions. I want to get from db some data ordered
I have problem with Activities navigation, searching works good. Activities hierarchy looks like that:
I have problem running my app on Android 2.3 (Gingerbread). The app has a
I have problem in some JavaScript that I am writing where the Switch statement
have problem with ampersand (&) How to search for the words (or sentences) that
I have problem while defining keys in resource file for globalization. i want to
i have problem with showing an UIView on app delegate ... there is no

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.