无法从 SelectList Razor Pages 中获取选定的值

Cannot get selected value from SelectList Razor Pages

我正在使用 Razor Pages 制作一个简单的 MovieApp。我有两个模型:Movie * - 1 Genre。我想要的是,当我导航到 Genres 页面和 select 特定流派时,我想显示所有电影的 List与此流派相关联,我希望能够 select 和 导航 流派到 电影 .

我遇到的问题是 Razor Pages 的 SelectList 不是 Binding selected reference 值(即Movies 的对象),但是当我有 MoviesIDs 时,我得到了正确的 selected 值。

这是我的代码:

Details.cshtml

<div>
<h4>Genre</h4>
<hr />
<form method="post">
    <dl class="row">
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Genre.GenreTitle)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Genre.GenreTitle)
        </dd>
    </dl>
    <div class="form-group" style="width: 250px;">
        <label asp-for="MoviesSL" class="control-label"></label>
        <select asp-for="SelectedMovie" class="form-control"
                asp-items="Model.MoviesSL">
        </select>
    </div>
    <span asp-validation-for="SelectedMovie.ID" class="text-danger"></span>
    <div>
        <input type="submit" value="Navigate" class="btn btn-info" />
    </div>
</form>

Details.cshtml.cs

    public Genre Genre { get; set; }
    [Display(Name = "Movies list")]
    public SelectList MoviesSL { get; set; }
    private Movie selectedMovie;
    [BindProperty]
    public Movie SelectedMovie 
    {
        get => selectedMovie;
        set
        {
            selectedMovie = value;
            //int g = 0;
        }
    }

    public async Task<IActionResult> OnGetAsync(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        Genre = await _context.Genre.Include(m => m.Movies).FirstOrDefaultAsync(m => m.GenreId == id);
        MoviesSL = new SelectList(Genre.Movies);

        //int g = 0;

        if (Genre == null)
        {
            return NotFound();
        }
        return Page();
    }

    public void OnPost()
    {
        var k = SelectedMovie;

        //int g = 0;
    }

Movie.cs

public class Movie
    {
        [Key]
        public int ID { get; set; }

        [Required]
        [StringLength(60, MinimumLength = 3)]
        public string Title { get; set; }

        [DataType(DataType.Date)]
        [Display(Name = "Release Date")]
        [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
        public DateTime ReleaseDate { get; set; }

        [Range(1, 300)]
        [DataType(DataType.Currency)]
        [Column(TypeName = "decimal(18,2)")]
        public decimal Price { get; set; }

        [Required]
        [StringLength(5)]
        [RegularExpression(@"^[A-Z]+[a-zA-Z0-9""'\s-]*$")]
        public string Rating { get; set; }

        public int GenreId { get; set; }
        public Genre Genre { get; set; }

        public override string ToString()
        {
            return Title;
        }
    }

Genre.cs

public class Genre
{
    public Genre()
    {
        Movies = new List<Movie>();
    }

    [Key]
    public int GenreId { get; set; }
    [Required]
    [StringLength(30, MinimumLength = 5)]
    [Display(Name = "Genre Title")]
    [RegularExpression(@"^[A-Z]+[a-zA-Z]*$")]
    public string GenreTitle { get; set; }

    [Display(Name = "Number of movies")]
    public ICollection<Movie> Movies { get; set; }

    public override string ToString()
    {
        return GenreTitle;
    }
}

当我 select 列表中的一部电影并尝试 “导航” 通过 post 方法时,我得到一个空的 电影 对象。

但是,当我使用 IDsSelectList 而不是 Movies 时,它可以正常工作:

要启动 SelectList,您需要 3 个属性:项目列表、属性 可用于 SelectList 的值的项目的名称,以及可用于显示 SelectList 的名称。

因此您必须修复 SelectList 代码:

 MoviesSL = new  SelectList(Genre.Movies,"ID","Title");

首先,<select></select>只会return它的选择值,它不能绑定model.And你可以使用MoviesSL = new SelectList(Genre.Movies,"ID","Title");作为Sergey said.And然后你可以 return 选定的电影 ID 到 post handler.And 然后从 _context.

获取带有 ID 的电影

1.remove

<span asp-validation-for="SelectedMovie.ID" class="text-danger"></span>

2.change

[BindProperty]
    public Movie SelectedMovie 
    {
        get => selectedMovie;
        set
        {
            selectedMovie = value;
            //int g = 0;
        }
    }

[BindProperty]
        public int SelectedMovie { get; set; }

3.change

public void OnPost()
    {
        var k = SelectedMovie;

        //int g = 0;
    }

public void OnPost()
    {
        Movie k = _context.Movie.Where(m=>m.ID== SelectedMovie).FirstOrDefault();

        //int g = 0;
    }