1

I've a collection of Cards in my EmployeeMasterA class, I'm not sure what is the correct way to append the collection in FormData object. What I want something like this :

var data = new FormData();
data.append('Cards[0].CardId', 1);
data.append('Cards[0].CardTitle', "Credit");
data.append('Cards[1].CardId', 2);
data.append('Cards[1].CardTitle', "Debit");

and this is my ajax request:

$.ajax({
    url: '/EmployeeMasterA/Create',        
    data: data,
    type: 'POST',        
    contentType: false,
    processData : false,        
    success: function (result) {
        if (result.Success == "1") {                
        }
    }
});

and my controller :

[HttpPost]
public ActionResult Create(EmployeeMasterA employeeMasterA)
{
    //get data and perform logics
}

EmployeeMasterA Class :

public class EmployeeMasterA
{
    public string EmployeeCode { get; set; }
    public virtual ICollection<Cards> Cards { get; set; }
}

EmployeeMasterA contains collection of Cards, here is the Cards Class :

public class Cards
{
    public int CardId { get; set; }
    public string CardTitle { get; set; }
}

How do I append the collection of Cards in the FormData object? So that it binds correctly with the EmployeeMasterA class.

Update

When I take a look in debug mode, the Cards collection is null, Why?

Bilal Ahmed
  • 1,043
  • 4
  • 17
  • 32
  • 2
    The code you have shown works perfectly and `Cards` contains 2 items that are correctly populated. If its not working for you, its either not the code your using or you have omitted some relevant code. –  Mar 21 '17 at 22:18
  • @StephenMuecke, You are right, I was doing a very silly mistake, `FormData` was getting reinitialized, that's why I was getting `null`, Thank you so much for your time :) – Bilal Ahmed Mar 22 '17 at 09:49

1 Answers1

0

Why are you using FormData? You can simply send json object of your data.

See this: How to receive JSON as an MVC 5 action method parameter

Community
  • 1
  • 1
Yashasvi
  • 197
  • 1
  • 3
  • 6