I have a form in a Create view. When I try to display a certain field, I get NullReferenceException: Object reference not set to an instance of an object.
. If I comment out the input control for ItemId, all is well. The label shows up, but obviously not the control.
Variety:
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; }
}
View:
@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>
....
....