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

The Archive Base Latest Questions

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

This question is similar, but with no conclusion and a self accepted answer of

  • 0

This question is similar, but with no conclusion and a self accepted answer of “no”: automatically generate javascript object model from c# object

This answer by Darin is a very good example of mapping an object to Json, although I am not sure if it works with complex models: https://stackoverflow.com/a/9007578/1026459

Here is an example of what I was looking at. Given this setup:

Viewmodel:

public class Avm
{
 public int id { get; set; }
 public string name { get; set; }
 public Foo Foo { get; set; }
}

public class Foo
{
 public int id { get; set; }
 public string description { get; set; }
 public List<Bar> Bars { get; set; }
}

public class Bar
{
 public int id { get; set; }
 public string metal { get; set; }
}

Is there a possible way to define this object model:

javascript object model:

<script>
 var Avm = {};
 Avm.id = @(Model.id);
 Avm.name = "@(Model.name)";
 Avm.Foo = {};
 Avm.Foo.id = @(Model.Foo.id);
 Avm.Foo.description = "@(Model.Foo.description)";
 Avm.Foo.Bars = [];
 var Bar = function(){
  return
  {
   id : var id,
   metal : var metal;
  };
 }
 var Bar0 = new Bar();
 Bar0.id = @(Model.Foo.Bars[0].id);
 Bar0.metal = "@(Model.Foo.Bars[0].metal)";
 Avm.Foo.Bars.push(Bar0);
 //and so on for each Bar.
</script>

Through a more precise method than just typing it all out by hand?

Or, perhaps through some sort of reflection in a helper so that it would become

<script>
 @Html.GenerateJsObjectModel(Model)
<script>

Which would call a helper method

    public static void GenerateJsObjectModel(
        this HtmlHelper html, dynamic model)
    {
      html.ViewContext.Writer.Write("var Avm = {};");
      html.ViewContext.Writer.Write("Avm.id = " + model.id + ");");
      //etc. (except obviously more generic instead of specific)
    }

Does this already exist in the framework? Would using a serialization process like JSON be superior here and if so what type of hook would be used to tie JSON in for this scenario?

What is the best practice approach for creating a complex model in javascript based on a viewmodel.

  • 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:53:02+00:00Added an answer on June 7, 2026 at 3:53 am

    I’m partial of using the JavaScriptSerializer.

    Not sure how to post Gists here so here is the link to the Gist with the pertinent pieces of code:

    https://gist.github.com/3043237

    Index.cshtml

    @model JSSerializerSample.Models.IndexViewModel
    @{
        ViewBag.Title = "Home Page";
    }
    
    <h2>@ViewBag.Message</h2>
    <p>
        To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
    </p>
    <div id="userName"></div>
    <div id="message"></div>
    <div id="complexProperty1"></div>
    
    <script>
        $(function () {
            var options = @Html.Raw(Model.AsJson());
            $("#userName").html(options.UserName);
            $("#message").html(options.Message);
            $("#complexProperty1").html(options.ComplexProperty.ComplexProperty1);
        });
    </script>
    

    ComplexViewModel.cs

    namespace JSSerializerSample.Models
    {
        public class ComplexViewModel
        {
            public string ComplexProperty1 { get; set; }
        }
    }
    

    IndexViewModel.cs

    namespace JSSerializerSample.Models
    {
        public class IndexViewModel : BaseViewModel
        {
            public string Message { get; set; }
            public string UserName { get; set; }
    
            public ComplexViewModel ComplexProperty { get; set; }
        }
    }
    

    BaseViewModel.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Script.Serialization;
    
    namespace JSSerializerSample.Models
    {
        public abstract class BaseViewModel
        {
            public string AsJson()
            {
                var serializer = new JavaScriptSerializer();
                return serializer.Serialize(this);
            }
        }
    }
    

    The complete code sample can be downloaded here: https://github.com/devlife/Sandbox/tree/master/JSSerializerSample

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

Sidebar

Related Questions

This Question is similar but the accepted answer solves it server side, I'm interested
This is similar to (but different from) this question . Here is some simple
This question is similar to mine, but does not contain an answer. I want
This question is similar to this one, but with an extra wrinkle: Auto-removing all
This question is similar to knockoutjs databind with jquery-ui datepicker , but instead of
I know this question is similar to several previous ones, but I can't find
Jquery Each Json Values Issue This question is similar to above, but not the
My question is similar to this MySQL question, but intended for SQL Server: Is
I found this similar question here , but this is really old. Was it
I have checked this similar question, but the suggestions did not solve my problem:

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.