我目前正在主控制器中做一个编辑类。似乎一切都很好,但是当我构建程序时出现错误
这是 MoviesController 中的 Edit 类:
public ActionResult Edit(int? Id)
{
if (Id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
MoviesData moviesData = db.MoviesData.Find(Id);
if (moviesData == null)
return HttpNotFound();
return View(moviesData);
}
(电影数据是数据库的表实例(
这是视图模型:
public class MovieViewModel
{
public int MovieID { get; set; }
public string MovieName { get; set; }
public string MovieDescription { get; set; }
public string MovieCategory { get; set; }
public string MovieYear { get; set; }
}
每次我尝试构建程序时,都会出现此错误:
"System.Data.Entity.DynamicProxies.MoviesData_4D99853B016ED5E65AF8566A78AEB274D91AD066D99E2B2FA50D6EA0156AAA21",但此字典需要类型为"WebApplication3.Models.MovieViewModel"的模型项
我了解错误的含义,但我不知道如何更改它。
您需要将 DTO 转换为视图模型:
MovieViewModel viewModel = new MovieViewModel()
{
MovieID = moviesData.MovieID (or maybe only .ID?),
MovieName = moviesData.MovieName
etc....
}
return View(viewModel );