How to assign LIST class models Nested in Web API 2.0

46 Views Asked by At

I need help, to Assign List Nested Web Api 2.0:

public HttpResponseMessage Post([FromBody] ZHitung1m respon1)

So far my code only can Assign First Class Model

I can assign and call = respon1.DistCode
but somehow I cannot assign and call = respon1.ProductCode

Here my Models class WEB Api :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace WebAPI.Models
{
    public class ZHitung1m
    {
        public string DistCode { get; set; }
        
        public List<ZHitung2m> Row { get; set; }

        public class ZHitung2m
        {
            public string ProductCode { get; set; }
        }

    }
}

Here is my API Controller use HttpResponseMessage Post =

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebAPI.Models;

namespace WebAPI.Controllers
{
    public class Zhitung1Controller : ApiController
    {

        [HttpPost]
        public HttpResponseMessage Post([FromBody] ZHitung1m respon1)
        {

            var variable1 = respon1.DistCode;

            if (variable1 == "anton")
            {
                respon1.DistCode = "budiman";
            }

            return Request.CreateResponse(HttpStatusCode.OK, respon1);

        }
        }
}

So far I just can only assign (respon1.DistCode)
How to call and assign (respon1.ProductCode)?

Without changing nested structure in my POSTMAN, here my postman Result: want keep nested structure like this

  1. Anyone know to call and assign (respon1.ProductCode)? So far I try
public class ZHitung2m : ZHitung1m
{
    public string ProductCode { get; set; }
}

Its change and broke postman structure that I want.

2.

 public class ZHitung1m : ZHitung2m
 {
     public string DistCode { get; set; }
     
     public List<ZHitung2m> Row { get; set; }

 }
 public class ZHitung2m
 {
     public string ProductCode { get; set; }
 }

Use this class models code, can call assign (respon1.ProductCode) but also change and broke postman structure that I want:

postman2

1

There are 1 best solutions below

1
David House On BEST ANSWER

Making your models inherit from each other doesn't seem like what you want to do at all, as they are representing different concepts, i.e. ZHitung1m contains a list of ZHitung2m, if you're inheriting one from the other you're saying ZHitung1m is a type of ZHitung2m or vice versa.

So no inheritance needed... then try something along these lines (changing variable names to something descriptive):

namespace WebAPI.Controllers
{
    public class Zhitung1Controller : ApiController
    {

        [HttpPost]
        public HttpResponseMessage Post([FromBody] ZHitung1m respon1)
        {

            var variable1 = respon1.DistCode;

            if (variable1 == "anton")
            {
                respon1.DistCode = "budiman";
            }

            foreach(var row in respon1.Row)
            {
                var variable2 = row.ProductCode;

                ... do something with variable2 or row here ...
            }

            return Request.CreateResponse(HttpStatusCode.OK, respon1);

        }
    }
}