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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:38:43+00:00 2026-06-05T21:38:43+00:00

I am trying to output JSON directly in my Razor view. The object being

  • 0

I am trying to output JSON directly in my Razor view. The object being serialized is IEnumerable<ItemDto> (see definition below). I want to serialize it to identical JSON that would be returned from an ApiController. Here is what I have tried:

  • When using @Json.Encode(theValue), it ignores the DataMember, so the properties get the wrong names (uppercase first letter).
  • When using DataContractJsonSerializer (and castingthe IEnumerable<ItemDto> to IList<ItemDto>), it outputs the ImageDto.Sizes as [{ Key: 'foo', Value: <object> }], but the ApiController correctly does { 'foo': <object> }.

Here are my classes:

    [DataContract]
    public class ItemDto
    {
        [DataMember(Name = "id")]
        public int Id { get; set; }

        [DataMember(Name = "title")]
        public string Title { get; set; }

        [DataMember(Name = "image")]
        public ImageDto Image { get; set; }

        [DataMember(Name = "price")]
        public decimal Price { get; set; }
    }

    [DataContract]
    public class ImageDto
    {
        [DataMember(Name = "id")]
        public Guid Id { get; set; }
        [DataMember(Name = "sizes")]
        public Dictionary<String, ImageSizeDto> Sizes { get; set; }
    }

    [DataContract]
    public class ImageSizeDto
    {
        [DataMember(Name = "url")]
        public string Url { get; set; }
        [DataMember(Name = "w")]
        public int Width { get; set; }
        [DataMember(Name = "h")]
        public int Height { get; set; }
    }

The objective is to avoid a separate GET request for data that is necessary on almost every page.

Desired JSON:

[{"id":6,
 "title":"Foo bar baz",
 "image":
    {"id":"fb2a3b4a-5ae5-4d9d-baff-72e107aa6e9c",
     "sizes":
        {"Original": {"url":"http://example.com/a", "w":-1, "h":-1},
         "Thumbnail": {"url":"http://example.com/b", "w":130, "h":73},
         "LargeThumbnail": {"url":"http://example.com/c", "w":220,"h":124},
         "Popup": {"url":"http://example.com/d", "w":256, "h":256},
         "FullWidth": {"url":"http://example.com/e", "w":930, "h":524}}},
 "price":79}]
  • 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-05T21:38:45+00:00Added an answer on June 5, 2026 at 9:38 pm

    You could use Json.NET:

    [DataContract]
    public class ItemDto
    {
        [DataMember(Name = "id")]
        public int Id { get; set; }
    
        [DataMember(Name = "title")]
        public string Title { get; set; }
    
        [DataMember(Name = "image")]
        public ImageDto Image { get; set; }
    
        [DataMember(Name = "price")]
        public decimal Price { get; set; }
    }
    
    [DataContract]
    public class ImageDto
    {
        [DataMember(Name = "id")]
        public Guid Id { get; set; }
        [DataMember(Name = "sizes")]
        public Dictionary<String, ImageSizeDto> Sizes { get; set; }
    }
    
    [DataContract]
    public class ImageSizeDto
    {
        [DataMember(Name = "url")]
        public string Url { get; set; }
        [DataMember(Name = "w")]
        public int Width { get; set; }
        [DataMember(Name = "h")]
        public int Height { get; set; }
    }
    
    class Program
    {
        static void Main()
        {
            var items = new[] 
            {
                new ItemDto
                {
                    Id = 6,
                    Title = "Foo bar baz",
                    Image = new ImageDto
                    {
                        Id = Guid.NewGuid(),
                        Sizes = new Dictionary<string, ImageSizeDto>
                        {
                            { "Original", new ImageSizeDto { Url = "http://example.com/a", Width = -1, Height = -1 } },
                            { "Thumbnail", new ImageSizeDto { Url = "http://example.com/b", Width = 130, Height = 73 } },
                        },
                    },
                    Price = 79
                }
            };
    
            Console.WriteLine(JsonConvert.SerializeObject(items));
        }
    }
    

    outputs:

    [
        {
            "id": 6,
            "title": "Foo bar baz",
            "image": {
                "id": "6a1c1d34-3e78-4303-8edd-d8541dd915e7",
                "sizes": {
                    "Original": {
                        "url": "http://example.com/a",
                        "w": -1,
                        "h": -1
                    },
                    "Thumbnail": {
                        "url": "http://example.com/b",
                        "w": 130,
                        "h": 73
                    }
                }
            },
            "price": 79
        }
    ]
    

    Note that in the ASP.NET MVC 4 RTM Json.Net will be the default serialzier. Until then simply swap the built-in serializer with Json.NET as illustrated in the Scott Hanselman’s blog post.

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

Sidebar

Related Questions

I am trying to access and output the JSON response: I want to get
I'm trying to write an object as JSON to my Asp.Net MVC View using
Trying to get a JSON output to work with jqGrid 'userdata' option. The example
I am trying to output some Java objects as JSON, they have List properties
I am trying to output results of 2 sql queries to one JSON file.
I'm trying to pull the field names in the header of some JSON output.
I am trying to get my datatable to take a POST JSON output from
I'm writing custom ActionFilterAttribute and trying to write some data directly into output stream
Trying to build dynamic output from json and using jq/template tmpl display rows/columns. Somehow
What I'm trying to achieve, is to output a json list that contains a

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.