我想创建有关电影的详细信息页面,但出现此错误,有点卡住了。我收到无法转换的类型错误。我确实知道该错误,但是我不知道如何解决它。
这是PAP实体是数据库实例的索引:
String address = addresses.get(0).getSubLocality();
String cityName = addresses.get(0).getLocality();
String stateName = addresses.get(0).getAdminArea();
这是我现在的详细信息类:
public class MoviesController : Controller
{
PAPEntities db = new PAPEntities();
public ActionResult Index()
{
MovieViewModel[] movies = db.MoviesData.Select(movie => new MovieViewModel
{
MovieID = movie.MovieID,
MovieName = movie.MovieName,
MovieDescription = movie.MovieDescription,
MovieCategory = movie.MovieCategory,
MovieYear = movie.MovieYear
}).ToArray();
return View(movies);
}
我不断在“ db.MoviesData.Find(Id);”上收到错误消息
这是MovieViewModel代码:
public ActionResult Details(int Id = 1)
{
MovieViewModel MovieVM = db.MoviesData.Find(Id);
return View(MovieVM);
}
这是剃须刀页面:
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; }
}
@model WebApplication3.Models.MovieViewModel[]
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Movies</h2>
<p>
@Html.ActionLink("Create New", "Create")
这是错误:
无法将类型'WebApplication3.Models.MoviesData'隐式转换为 'WebApplication3.Models.MovieViewModel'
MoviesData是数据库的表
答案 0 :(得分:1)
您应该将数据库(MoviesData
)模型映射到业务模型(MovieViewModel
)
public ActionResult Details(int Id = 1)
{
MoviesData movie = db.MoviesData.Find(Id);
MovieViewModel MovieVM = new MovieViewModel();
MovieVM.MovieID = movie.MovieID;
MovieVM.MovieName = movie.MovieName;
MovieVM.MovieDescription = movie.MovieDescription;
MovieVM.MovieCategory = movie.MovieCategory;
MovieVM.MovieYear = movie.MovieYear;
return View(MovieVM);
}
答案 1 :(得分:0)
在您的dput(head(plotData,20))
structure(list(Date = structure(1:20, .Label = c("2001-12-03",
"2001-12-04", "2001-12-05", "2001-12-06", "2001-12-07", "2001-12-10",
"2001-12-11", "2001-12-12", "2001-12-13", "2001-12-14", "2001-12-17",
"2001-12-18", "2001-12-19", "2001-12-20", "2001-12-21", "2001-12-24",
"2001-12-25", "2001-12-26", "2001-12-27", "2001-12-28", "2001-12-31",
"2002-01-01", "2002-01-02", "2002-01-03", "2002-01-04", "2002-01-07",
"2019-05-22", "2019-05-23"), class = "factor"), Arima = c(-0.0134052258713131,
-0.00542641764174324, 0.0128513670753771, 0.0282761455973665,
0.0179931884968989, 0.0281714817318116, 0.0435962602538011, 0.0462004298658309,
0.0194592964361352, 0.0248069155406948, 0.032807001046888, 0.0381120657516546,
0.0381120657516546, 0.030090589527961, -0.0146168717909267, -0.00630652663076437,
-0.00630652663076437, -0.00630652663076437, 0.0100429785563596,
0.0100429785563596), BuyHold = c(-0.0134052258713131, -0.00542641764174324,
0.0128513670753771, 0.0282761455973665, 0.0384544388322794, 0.0281714817318116,
0.0125050470584384, 0.0151092166704679, -0.0116319167592278,
-0.0170082867113405, -0.0090082012051471, -0.00370313650038065,
-0.00370313650038065, -0.0117246127240743, -0.056432074042962,
-0.0481217288827996, -0.0481217288827996, -0.0481217288827996,
-0.0317722236956757, -0.0317722236956757)), row.names = c(NA,
20L), class = "data.frame")
方法中,ActionResult Details
返回类型为db.MoviesData.Find(Id)
但变量类型为WebApplication3.Models.MoviesData
的对象。这就是为什么您要例外。
您需要手动投射。因此,您可以这样编写代码,
MovieViewModel