我在“创建”视图中有一个表单。当我尝试显示某个字段时,我得到NullReferenceException: Object reference not set to an instance of an object.
。如果我注释掉ItemId的输入控件,一切都很好。标签显示,但显然不是控件。
品种:
public class Variety
{
[Key]
public int ItemId { get; set; }
public int SpeciesId { get; set; }
public virtual Species species { get; set; }
[DataType(DataType.Currency, ErrorMessage = "Please ender a currency value")]
[DisplayFormat(DataFormatString = "{0:C}", ApplyFormatInEditMode = true)]
public double Price { get; set; }
[Required(ErrorMessage = "You must enter a variety.")]
[Display(Name = "Variety")]
public string VarietyName { get; set; }
}
InventoryItem:
public class InventoryItem
{
[Key]
[Display(Name = "Inventory Id")]
public int InventoryId { get; set; }
[Required(ErrorMessage = "Item Id Required")]
[Display(Name = "Item Id")]
public int ItemId { get; set; }
public virtual Variety variety { get; set; }
[Required(ErrorMessage = "Quantity Required")]
public int Quantity { get; set; }
}
查看:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>New Inventory Item</h4>
<hr />
<div class="form-group">
<label for="ItemId", class = "control-label col-md-2">Item Id</label>
<div class="col-md-10">
<input type="text" value="@Model.ItemId" id="ItemId" readonly />
</div>
</div>
....
....