Save array list into MySQL Database using Model binding approach in MVC
Please let me know how to save 'PearlProject' property values (array list) from the Controller into DB?
Do I also have to modify the Model class?
I have a ViewModel Class:
namespace WebApp.ViewModel
{
public class MappingStudyViewModel
{
public string StudyId { get; set; }
[NotMapped]
public string StudyId1ToMany { get; set; }
public List<HdrFdpStudy> StudyList { get; set; }
public string ScenarioId { get; set; }
public List<HdrFdpScenarios> ScenarioList { get; set; }
public string ProjectId { get; set; }
[NotMapped]
public string[] MultipSelectProjectId { get; set; }
public List<HdrPearlProject> ProjectList { get; set; }
public IEnumerable<Mapping> Mapping { get; set; }
public int Id { get; set; }
public IEnumerable<MappingDetails> MappingDetails { get; set; }
}
}
Controller Code:
[HttpPost]
public IActionResult Save(MappingStudyViewModel mappingStudyViewModel, string rdMappingType, string chkStatus)
{
mappingStudyViewModel.ProjectList = BindProjectDDL();
mappingStudyViewModel.MultipSelectProjectId = [ 1, 4, 9, 3];
var getMultiProjectName = (from x in mappingStudyViewModel.ProjectList.ToList()
where mappingStudyViewModel.MultipSelectProjectId.Contains(x.ProjectId)
select x.ProjectName).Distinct().ToList();
var mMapping = new Mapping()
{
MappingType = rdMappingType,
RunState = chkStatus
};
_repository.Add(mMapping);
int LatestMappingId = mMapping.MappingId;
var mMappingDetails = new MappingDetails()
{
MappingId = LatestMappingId,
FdpStudy = getStudyNameFor1ToMany,
FdpScenario = getScenarioName.Text.ToString(),
PearlProject = ?? // how to save 'getMultiProjectName' array list items into DB ??
};
}
Thank you in advance