0

I have a web application, built with MVC, Web API 2. Server side - C#, client - JS.

I have a problem with receiving class objects in my controller. Suppose, there is an hierarchy of classes:

class Person
{
    public string Name { get; set; }
}

class Student : Person
{
    public double Grade { get; set; }
}

class Professor : Person
{
    public string University { get; set; }
}

In my controller, I have a method, which receives an object of the Person type:

[HttpPost]
public HttpResponseMessage AddPerson(Person person)
{
    ...
}

The Client is sending the correct object (either Student or Professor), but the controller does not know how to deserialize it from Json and always gets null.

Any ideas of how can I cause the server to deserialize the parameters correctly?

dario
  • 5,149
  • 12
  • 28
  • 32
Vadim
  • 1
  • 1
  • Just save yourself the trouble and add different endpoints. – Stilgar Feb 12 '15 at 14:47
  • Generic actions are a possible solution although a major pita, you could create a custom model binder... http://www.codeproject.com/Articles/605595/ASP-NET-MVC-Custom-Model-Binder – Paul Zahra Feb 12 '15 at 14:57

0 Answers0