Mindless
Mindless

Reputation: 2357

Save List View Models to Database

I have the following view model

public class MyCVViewModel
{
    [Required]
    [StringLength(100, ErrorMessage = "Resume Title cannot exceed 100 characters.")]
    [Display(Name = "Resume Title")]
    public string ResumeTitle { get; set; }
    [Required]
    [StringLength(1000, ErrorMessage = "Personal Statment cannot exceed 1000 characters.")]
    [Display(Name = "Personal Statement")]
    public string Statement { get; set; }

    public List<MyCompanyViewModel> Companies { get; set; }
}

public class MyCompanyViewModel
{
    [Required]
    [StringLength(100, ErrorMessage = "Company Name cannot exceed 100 characters.")]
    [Display(Name = "Company Name")]
    public string CompanyName { get; set; }
    [Required]
    [StringLength(100, ErrorMessage = "Job Title cannot exceed 100 characters.")]
    [Display(Name = "Job Title")]
    public string JobTitle { get; set; }
    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "Start Date")]
    public DateTime JobStartDate { get; set; }
    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "End Date")]
    public DateTime JobEndDate { get; set; }
    [Required]
    [StringLength(1000, ErrorMessage = "Job Description cannot exceed 1000 characters.")]
    [Display(Name = "Job Description")]
    public string JobDescription { get; set; }
}

I have a controller which saves a list of companies to the view model:

public ActionResult Create()
{
    var model = new MyCVViewModel()
    {

    };

    model.Companies = new List<MyCompanyViewModel>();
    model.Companies.Add(new MyCompanyViewModel() { CompanyName = "Company1", JobTitle = "Gyprocker", JobDescription = "Hello" });
    model.Companies.Add(new MyCompanyViewModel() { CompanyName = "Company2", JobTitle = "Gyprocker2", JobDescription = "Hello" });
    model.Companies.Add(new MyCompanyViewModel() { CompanyName = "Company3", JobTitle = "Gyprocker3", JobDescription = "Hello" });
    model.Companies.Add(new MyCompanyViewModel() { CompanyName = "Company4", JobTitle = "Gyprocker4", JobDescription = "Hello" });

    return View("~/Views/Dashboard/CV/Create.cshtml", model);
}

Now I am not exactly sure how to post this list view model back to the database, I am using entity framework code first approach and this is what I tried:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(MyCVViewModel model)
{
    if (!ModelState.IsValid)
    {
        return View("~/Views/Dashboard/CV/Create.cshtml", model);
    }
    else
    {
        var cv = new CV();

        cv.Title = model.ResumeTitle;
        cv.Statement = model.Statement;
        cv.Id = User.Identity.GetUserId();
        cv.Companies = new List<Company>();
        foreach (var company in model.Companies)
        {
            cv.Companies.Add(company); //this line has error saying cannot convert viewmodel to entity
        }

        repository.AddCV(cv);

        return RedirectToAction("List");
    }
}

So what is the best way to add a list viewmodel to the database?

Update

foreach (var company in model.Companies)
{
    cv.Companies.Add(
            new Company
            {
                CompanyName = company.CompanyName,
                JobTitle = company.JobTitle,
                StartDate = company.JobStartDate,
                EndDate = company.JobEndDate,
                Description = company.JobDescription
            });
}

Tried this and it works, is there any other approaches?

Upvotes: 0

Views: 375

Answers (1)

user3559349
user3559349

Reputation:

In the foreach loop you need to initialize a new Company

foreach (var item in model.Companies)
{
  Company company = new Company()
  {
    CompanyName = item.CompanyName,
    JobTitle = item.JobTitle,
    // etc for other properties
  };
  cv.Companies.Add(company);
}

Upvotes: 1

Related Questions