我的视图模型遇到了一些问题。我有一个 create 方法,并且 create 方法是 post。在我的视图模型中,我发送了两个类 ProjectModel 和 ActivityLogModel。我遇到的问题是下面的错误。它希望将数据作为 ProjectViewModel 返回,但数据来自 ProjectModel。如何更正此问题,以便将数据作为正确的类型返回。
传入字典的模型项的类型为“GlobalNaming.Models.ProjectModel”,但此字典需要“GlobalNaming.Models.ProjectViewModel”类型的模型项。
视图模型
public List<ProjectModel> Project { get; set; }
public List<ActivityLogModel> Activity { get; set; }
控制器
public ActionResult Create()
{
ProjectViewModel projectvm = new ProjectViewModel();
ProjectModel project = new ProjectModel();
project.ProjectOwner = UserName;
project.Description = _description;
project.Timing = _timing;
project.Availability = _availability;
project.Positioning = _positioning;
project.Competitor = _competitor;
projectvm.Project = project;
return View(projectvm);
}
[HttpPost]
public ActionResult Create(ProjectModel pm, HttpPostedFileBase files)
{
pm.CreatedDate = dtNow;
pm.UpdatedDateTime = dtNow;
pm.ProjectStatus = "Initiation";
pm.Rejected = false;
if (ModelState.IsValid)
{
db.Projects.Add(pm);
db.SaveChanges();
UpdateSupportDocs(pm, files);
return RedirectToAction("Index");
}
return View(pm);
}