1

I am trying to pass a List to my Web API but I am getting a null value always. I am converting the list in to a string before passing it to the API. How can I send a list to API and Update the data correctly? Please help me to figure out the correct code. Class: MatrixProcessData.cs

namespace AHS.IBP.BO
{
   [Serializable, DataContract]
public class MatrixProcessData
{
    #region Variables
   // private int _id;
    private int _processYear;
    private DateTime _processDate;



    #endregion

    #region Constructor
    public MatrixProcessData()
    {
       // _id = 0;
        _processYear = 0;
        _processDate = DateTime.Today;


    }
    #endregion

    #region Properties
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Key]
    // [DataMember]
    //public int ID { get { return _id; } set { _id = value; } }
    [DataMember]
    public int ProcessYear
    {
        get { return _processYear; }
        set { _processYear = value; }
    }
    [DataMember]
    public DateTime ProcessDate
    {
        get { return _processDate; }
        set { _processDate = value; }
    }

    #endregion
 }
  }

Method to call API:

public void ProcessData()
    {
        DataSet dsProcessData = new DataSet();
        dsProcessData = new TempMatrixClass().GetProcessData(DateTime.Now.Year,771);
        List<MatrixProcessData> li = new List<MatrixProcessData>();
        if (dsProcessData.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in dsProcessData.Tables[0].Rows)
            {

                MatrixProcessData mtrixProcessData = new MatrixProcessData();

                mtrixProcessData.ProcessYear = Convert.ToInt32(dr["ProcessYear"]);
                mtrixProcessData.ProcessDate = Convert.ToDateTime(dr["ProcessDate"]);



                li.Add(mtrixProcessData);

            }
            string apiUrl = string.Format("{0}api/MatrixProcessData/{1}/", aplilink, 2019);


            HttpClient httpClient = new HttpClient();
            httpClient.MaxResponseContentBufferSize = 256000;

            string strObject = Newtonsoft.Json.JsonConvert.SerializeObject(li, Newtonsoft.Json.Formatting.None);

            HttpContent httpContent = new StringContent(strObject, Encoding.UTF8, "application/json");

            var response = httpClient.PutAsync(apiUrl, httpContent, new System.Threading.CancellationToken(false)).Result;

            if (response != null && response.IsSuccessStatusCode)
            {
                var data = response.Content.ReadAsStringAsync().Result;
               // mtrixProcessData = Newtonsoft.Json.JsonConvert.DeserializeObject<MatrixProcessData>(data);
            }

        }
    }

Controller PUT API Method (List) is getting null: MatrixProcessDataController.cs

 [Route("{processyear}")]
    [ResponseType(typeof(List<MatrixProcessData>))]
    public IHttpActionResult PutmatrixProcessData(int processyear, List<MatrixProcessData> matrixProcessData)
    {
        foreach (var item in matrixProcessData)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Entry(matrixProcessData).State = EntityState.Modified;

            if (!matrixProcessDataExists(item.ProcessYear, item.ScenarioID, item.ModuleID, item.MeasureID, item.SKUID, item.ProcessDate))
            {
                db.MatrixProcessDatas.Add(item);
            }
            db.SaveChanges();
        }
        return StatusCode(HttpStatusCode.NoContent);
    }

1 Answer 1

1

Not sure if you have any other problem with your Processing code, but for the Controller code, you need to specify where the data binding happens, depending on how you make the HTTP request. You may need [FromBody] or [FromForm]:

[Route("{processyear}")]
[ResponseType(typeof(List<MatrixProcessData>))]
public IHttpActionResult PutmatrixProcessData([FromRoute]int processyear, [FromBody] List<MatrixProcessData> matrixProcessData)
{
    ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

@user3510330 Why? What is your HTTP request and what is the structure of your MatrixProcessData class?
@user3510330 and FromRoute is not for the list, it's for the processyear, as you have it in your URL.
namespace AHS.IBP.BO { [Serializable, DataContract] public class MatrixProcessData { #region Variables // private int _id; private int _processYear; #endregion #region Constructor public MatrixProcessData() { // _id = 0; _processYear = 0; _processDate = DateTime.Today; } #endregion #region Properties [DatabaseGenerated(DatabaseGeneratedOption.None)] [Key] // [DataMember]
@user3510330 Please edit your question and add the class declaration and the HTTP request body.
Class Structure isn't List Type.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.