将列表视图模型保存到数据库

时间:2014-12-08 01:12:45

标签: c# asp.net-mvc asp.net-mvc-viewmodel

我有以下视图模型

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; }
}

我有一个控制器,可以将公司列表保存到视图模型中:

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);
}

现在我不确定如何将此列表视图模型发布回数据库,我使用的是实体框架代码第一种方法,这就是我尝试过的方法:

[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");
    }
}

那么将列表视图模型添加到数据库的最佳方法是什么?

更新

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
            });
}

试过这个并且它有效,还有其他方法吗?

1 个答案:

答案 0 :(得分:1)

foreach循环中,您需要初始化新的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);
}