为什么这个文本字段正确地发送数据但是它被接收为Null并且Visual Studio引发了异常?

问题描述 投票:0回答:1

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

enter image description here

enter image description here

这是电影模型

 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>
}
c# asp.net asp.net-mvc entity-framework model-binding
1个回答
2
投票

看看图标,Genre_Id成员看起来与众不同。那是因为它是一个领域,而不是一个属性。

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

© www.soinside.com 2019 - 2024. All rights reserved.