I am facing a challenge where I need to transfer an object from the view to the controller, and the model comprises a list of objects, each containing another list of complex objects. Let's consider the following models:
public class CourseVm {
public IList<CousreAttendance> CourseAttendances { get; set;}
}
public class CourseAttendance {
public int StudentId { get; set; }
public List<SessionAttendance> SessionAttendances { get; set; }
}
public class SessionAttendance {
public int SessionId { get; set; }
public bool IsPresent { get; set; }
}
Sending a list of objects without inner-list properties works fine. However, sending the CourseVm
object to the controller always results in failure. The desired format for input values is shown below:
// Before submitting the form to the controller
form.append('<input type="hidden" name="CourseAttendances.Index" value="0" />'+
'<input type="hidden" name="CourseAttendances[0].StudentId" value="5" />'+
'<input type="hidden" name="CourseAttendances[0].SessionAttendances.Index" value="0" />' +
'<input type="hidden" name="CourseAttendances[0].SessionAttendances[0].IsPresent" value="true" />' +
'<input type="hidden" name="CourseAttendances[0].SessionAttendances[0].SessionId" value="555" />'
// Replicate this logic for SessionAttendances[1..n]
);
I prefer not to utilize @Html.HiddenFor()
due to certain reasons, therefore, I need to generate hidden inputs using jQuery. Can someone guide me on how to create the correct input format? Thank you.