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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:48:49+00:00 2026-06-16T03:48:49+00:00

Im using MVC 4 my ActionController recives the following Json: { MainId: 1, Actions:

  • 0

Im using MVC 4 my ActionController recives the following Json:

{
    "MainId": 1,
    "Actions": [
        {
            "Attribute1ClassA": 1,
            "Attribute2ClassA": 2
        },
        {
            "Attribute1ClassB": 3,
            "Attribute2ClassB": 4
        },
        {
            "Attribute1ClassC": 5
        }
    ]
}

and the Controller:

[HttpPost]
public ActionResult Commit(ActionsSummaryViewModel summary)
{
//DO stuff
}

and declaration for classes:

public ActionsSummaryViewModel
{
    public int MainId {get;set;}
    public IList<MainClass> {get;set;}
}

public class MainClass
{
}

public class ClassA : MainClass
{
   public int Attribute1ClassA {get;set;}
   public string Attribute2ClassA {get;set;}
}

public class ClassB : MainClass
{
   public int Attribute1ClassB {get;set;}
   public string Attribute2ClassB {get;set;}
}

public class ClassC : MainClass
{
   public int Attribute1ClassC {get;set;}
}

So now, how can i manage the deserialization for the MainClass when the action controller receive the JSON ? because when i call the action the list items are null.

if part of the solution is Json.NET, how i can implement for MVC 4 controllers?

Thanks for your help.

  • 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-16T03:48:50+00:00Added an answer on June 16, 2026 at 3:48 am

    You need a property or set of properties from which you can determine which type the class is to use this method. Using JSON.NET, I deserialize the incoming JSON as a dynamic object, then check the common property, determine the type, and deserialize the value again this type using my model type:

    // I'm assuming here you've already got your raw JSON stored in 'value'
    // In my implementation I'm using the Web API so I use a media formatter,
    // but the same principle could be applied to a model binder or however 
    // else you want to read the value.
    
    dynamic result = JsonConvert.DeserializeObject(value);
    switch ((string)result.type)
    {
    case "typeone":
    return JsonConvert.DeserializeObject<ModelOne>(value);
    // ...
    default: return null;
    }
    

    There’s a little bit of extra overhead here because you’re deserializing twice, but it’s worth it in most cases to me because it’s easy to understand what’s going on and add new types as needed.

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

Sidebar

Related Questions

When using MVC, which controller should Terms of Service, Privacy, Help, About actions belong
I have the following code in my zf2 controller: <?php namespace Accounting\Controller; use Zend\Mvc\Controller\ActionController,
I´m using mvc futures 2 with WebApiEnabled for XML and JSON support. But due
I'm using MVC with /<module>/<controller>/<action>/ have a module at example.com/module/whatever, and I need to
I have an application using MVC. In my controller I have their a viewdata[]
When using MVC you are calling actions that then load a view and a
Using MVC 3, a user gets to a URL like app/Controller/Details/123 An $.ajax request
When using MVC, returning adhoc Json was easy. return Json(new { Message = Hello});
(Using MVC 2) From inside my controller action, I need to display the url:
Im using MVC-Viewmodel, EF Model first on my project. I have a foreach loop

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.