为什么此文本字段可以正确发送数据,但收到的数据为 Null 并且 Visual Studio 引发异常?

Why does this text-field send data properly but it is received as Null and Visual Studio raises exception?

我将数据从视图发送到操作。 Chrome 浏览器显示 Genre_Id 已正确发送 正如您所看到的附加屏幕截图但是当操作接收到它时,Visual Studio 引发异常并且显示为 Null / 0 如您所附的屏幕截图所示。那为什么会这样呢?

这是电影模特

 public class Movie
{
    public int Id { get; set; }

    [Required]
    [StringLength(255)]
    public string Name { get; set; }

    [Required]
    [Display(Name="Created on")]
    public DateTime DateAdded { get; set; }

    [Required]
    [Display(Name = "Release Date")]
    public DateTime ReleaseDate { get; set; }

    [Required]
    [Display(Name = "Number in Stock")]
    public int NumberInStock { get; set; }

    public Genre Genre { get; set; }

    [Display(Name="Genre")]
    public int Genre_Id;

}

这是表格

@using(Html.BeginForm("Save", "Movies"))
{
<div class="form-group">
    @Html.LabelFor(m => m.Movie.Name)
    @Html.TextBoxFor(m => m.Movie.Name, new { @class="form-control"})
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Movie.DateAdded)
    @Html.TextBoxFor(m => m.Movie.DateAdded, new { @class="form-control"})
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Movie.ReleaseDate)
    @Html.TextBoxFor(m => m.Movie.ReleaseDate, new { @class="form-control"})
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Movie.NumberInStock)
    @Html.TextBoxFor(m => m.Movie.NumberInStock, new { @class="form-control" })
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Movie.Genre_Id)
    @Html.TextBoxFor(m => m.Movie.Genre_Id, new { @class="form-control"})
</div>
@Html.HiddenFor(m => m.Movie.Id)
<button  type="submit" class="btn btn-primary">Save</button>
}

看图标,Genre_Id 成员看起来不一样。那是因为它是一个字段,而不是 属性.

MVC 要求模型成员是属性。将 { get; set; } 添加到其声明中。