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?

2
  • Just save yourself the trouble and add different endpoints. Commented Feb 12, 2015 at 14:47
  • Generic actions are a possible solution although a major pita, you could create a custom model binder... codeproject.com/Articles/605595/ASP-NET-MVC-Custom-Model-Binder Commented Feb 12, 2015 at 14:57

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.