I have several model classes inherited from common base (e.g. Class1
, Class2
inherits from CommonClass
). I would like to write separate overloaded controller action which would have each of these model classes as parameter. For example:
[HttpPost]
public JsonResult MyAction(Class1 data)
{
// handle data here
}
[HttpPost]
public JsonResult MyAction(Class2 data)
{
// handle data here
}
Is it somehow possible to achieve that? When I try to simply write those action methods, MVC throws System.Reflection.AmbiguousMatchException
.
I have also tried to write single action with base class as parameter and then cast data to specific class, but this gives me null
.
[HttpPost]
public JsonResult MyAction(CommonClass data)
{
if(data.IsSomething)
{
var castedData = data as Class1;
// process casted data
}
else
{
var castedData = data as Class2;
// process casted data
}
}