S
S
saxer2017-01-03 15:35:50
ASP.NET
saxer, 2017-01-03 15:35:50

How to correctly convert an inherited object to Json in asp net mvc?

Let's say we have a base class

public class ProductDto
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual List<PriceDto> Prices { get; set; }
    }

And also the class inherited from it:
public class ProductVm:ProductDto
{
public new List<PriceVm> Prices { get; set; }
}

When creating a successor class and trying to translate it into JSON, I get two Prices properties of different types, is it possible to somehow form JSON correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Dudnik, 2017-01-12
@schart

The code itself looks quite crutch.
Using new to override method properties is worth it if it is __really__ very necessary and there is no other way to do it.
As far as I understand, PriceVm inherits from PriceDto
Then, for example, you can do this:

public abstract class ProductDtoBase<T> where T : PriceDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual List<T> Prices { get; set; }
}

public class ProductDto : ProductDtoBase<PriceDto>
{
}

public class ProductVm : ProductDtoBase<PriceVm>
{
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question