Http request send child type

240 views Asked by At

I've got an MVC web application that uses Angular at the javascript level. I'm trying to make an HTTP request with an object that looks similar to this :

public class Template
{
    public string Id { get; set; }
    //other properties
    public Enum Status { get; set; } //can be either of type InboundEnum or OutboundEnum
}

The problem is I have two possible Enum types (one called InboundEnum and one called OutboundEnum). Normally, when I use angular's $http.post() the request properly converts other JSON into the correct Enum value since it has a specific Enum (like InboundEnum).

The two Enums:

public enum InboundEnum
{
    Undefined = 0,
    Deleted,
    Saved,
    Pending,
    Completed,
    Cancelled
};

public enum OutboundEnum
{
    Undefined = 0,
    Deleted,
    Saved,
    Rejected,
    InTransit,
    Completed,
    Cancelled
};

Currently, the javascript object I'm sending looks like this:

{
    id: 'Id001`,
    status: 'Saved' //I know it's currently ambiguous which enum to use.  Solution?
}

How can I get the correctly typed values into the MVC API controller (like InboundEnum.Saved or OutboundEnum.Canceled)? Is there a more elegant way of handling the proper type of Enum than also appending an Template.EnumType property and using switch statements to get the right Enum?

If I've got an anti-pattern going here please let me know that too.

1

There are 1 answers

3
ShankarSangoli On

If you can use JSON.NET in your application then it can solve your problem. You don't have to pass the enum type in the request but have to specify certain attributes in your model on the server side.

public class Template
{
    public string Id { get; set; }
    //other properties
    [JsonConverter(typeof(StringEnumConverter))]
    public Enum Status { get; set; }
}