0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.