I have a model which contains lists of other models, which also contains lists of other model types like the following for example:
public class Report
{
public string ReportId {get; set;}
public List<ReportOutput> ReportOutputs;
}
//output types = PDf, csv, txt, etc.
public class ReportOutput
{
public int OutputType {get; set;}
public List<DeliveryMethod> DeliveryMethods;
}
//delivery methods = email, ftp, etc.
public class DeliveryMethod
{
public string MethodName {get; set;}
}
I have a view that dynamically creates elements based on the contents of these objects using jQuery. The view allows users to check checkboxes to add or remove different outputs and within each output, different delivery methods. I need to know how to get the information back to the controller to load into a model to send back to the database. From reading around, it seems that I would just need to instantiate a model in the controller, then give each element in the view a name which corresponds to the model’s properties and than call the controller function to retrieve all the data and continue the saving with the newly filled model, but my attempts at creating this controller function have failed.
So my question is, how would I create the controller function and do I just need to use the corresponding names on the html elements to allow a model to be created based on the information on the view?
If you follow the below pattern in generating name of form fields, the default model binder will take care of the rest else you have to do the model binding process yourself (tough job!).
ReportId,
ReportOutputs[0].OutputType,
ReportOutputs[0].DeliveryMethods[0].MethodName,
ReportOutputs[0].DeliveryMethods[1].MethodName,
ReportOutputs[1].OutputType,
ReportOutputs[1].DeliveryMethods[0].MethodName,