ASP.NET MVC错误无法将lambda表达式转换为类型' String'因为它不是代表类型
查看 - movieform.cshtml
:
<div class="form-group">
@Html.LabelFor(m => m.Movie.GenreId)
@Html.DropDownList(m => m.Movie.GenreId, new SelectList(Model.Genres, "Id", "Name"), "Select a Genre", new { @class = "form-control" })
</div>
查看模型 - movieformviewModel
:
public class MovieFormViewModel
{
public IEnumerable<Genre> Genres { get; set; }
public Movies Movie { get; set; }
}
模型类:
public class Movies
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
[Required]
public Genre Genre { get; set; }
public int GenreId { get; set; }
public DateTime ReleaseDate { get; set; }
public DateTime DateAdded { get; set; }
public int NumberInStock { get; set; }
}
public class Genre
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
}
控制器 - MOVIESSCONTROLLER
:
public ActionResult NewMovie(int id)
{
var movie = _context.Movies.SingleOrDefault(m => m.Id == id);
var viewModel = new MovieFormViewModel
{
Movie = movie,
Genres = _context.Genres.ToList()
};
return View("MovieForm", viewModel);
}
我的DBSET是:我
public DbSet<Genre> Genres { get; set; }
唯一的问题是我遇到的问题我的视图(movieform.cshtml
)。
这条标记
@Html.LabelFor(m => m.Movie.GenreId)
不会导致任何错误,但是这
@Html.DropDownList(m => m.Movie.GenreId, new SelectList(Model.Genres, "Id", "Name"), "Select a Genre", new { @class = "form-control" })
会引发错误
无法将lambda表达式转换为'字符串',因为它不是委托类型。
此错误在m.movie.gerreid
中,该错误在上一行中工作正常。
View - MovieForm.cshtml
:
<div class="form-group">
@Html.LabelFor(m => m.Movie.GenreId)
@Html.DropDownList(m => m.Movie.GenreId, new SelectList(Model.Genres, "Id", "Name"), "Select a Genre", new { @class = "form-control" })
</div>
View model - MovieFormViewModel
:
public class MovieFormViewModel
{
public IEnumerable<Genre> Genres { get; set; }
public Movies Movie { get; set; }
}
Model classes:
public class Movies
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
[Required]
public Genre Genre { get; set; }
public int GenreId { get; set; }
public DateTime ReleaseDate { get; set; }
public DateTime DateAdded { get; set; }
public int NumberInStock { get; set; }
}
public class Genre
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
}
Controller - MoviesController
:
public ActionResult NewMovie(int id)
{
var movie = _context.Movies.SingleOrDefault(m => m.Id == id);
var viewModel = new MovieFormViewModel
{
Movie = movie,
Genres = _context.Genres.ToList()
};
return View("MovieForm", viewModel);
}
My DbSet is:
public DbSet<Genre> Genres { get; set; }
The only problem I have is in my view (MovieForm.cshtml
).
This line of markup
@Html.LabelFor(m => m.Movie.GenreId)
doesn't cause any errors, however this
@Html.DropDownList(m => m.Movie.GenreId, new SelectList(Model.Genres, "Id", "Name"), "Select a Genre", new { @class = "form-control" })
throws an error
Cannot convert lambda expression to type 'string' because it is not a delegate type.
This error is in m.Movie.GenreId
which was was working fine in the previous line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
下拉列表
更改为下拉列表
。Change
DropDownList
toDropDownListFor
.