StrugglingCoder
StrugglingCoder

Reputation: 5021

How to send an array items from UI to MVC Controller

I have an array of objects in UI that is being sent to MVC Controller . The Array of objects look like :

`DoorId`,`DoorName` and an array of `Schedules`. `Schedules` array has `ScheduleId` and `ScheduleName`.

enter image description here

Now how to send it to MVC Controller ? So that , every DoorId and it's associated ScheduleId can be extracted separately to form another obeject ?

Presently , I am sending the DoorId Array and the ScheduleId array separately ,

But I do not want to do that . I want to send the entire array itself.

   public async Task<ActionResult> AddGroup(string[] DoorIds, string[] scheduleEntity)//AccessGroupEntity entity, string accountId
    {
        GroupEntity groupEntity = new GroupEntity();


        var doorScheduleList = new List<DoorInfoEntity>();

        for(int i=0;i< DoorIds.Length;i++)
        {
            doorScheduleList.Add(new DoorInfoEntity()
            {
                DoorId = DoorIds[i],
                ScheduleId = scheduleEntity[i]

            });
        }

        accessGroupEntity.DoorItems = doorScheduleList;

And then Parse it as Doors[index].DoorId and Doors[index].ScheduleId to form 'DoorInfoEntity` object.

How to do it ?

I tried object[] Doors but it says Object does not contain a definition for DoorId or ScheduleId.

Upvotes: 0

Views: 88

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239250

You need an object graph in C# that the modelbinder can bind the posted data to which mimics the object graph you have in your JavaScript. For example:

public class Door
{
    public Guid DoorId { get; set; }
    public string DoorName { get; set; }
    public List<Schedule> Schedules { get; set; }
    ...
}

public class Schedule
{
    public Guid ScheduleId { get; set; }
    ...
}

Then, you accept the root class as a param in your action:

public async Task<ActionResult> AddGroup(List<Door> doors)

The modelbinder, then, will create the object graph server-side from the posted data.

Upvotes: 1

Related Questions