我正在使用Entity Framework,代码优先,我在控制器中有这个代码
_auctionContext.AddToAuctionEvents(new AuctionEvent
{
Title = model.Title,
Description = model.Description,
Starts = model.Starts,
Ends = model.Ends,
DepositAmount = model.DepositAmount,
Currency = _auctionContext.Currency.Where(c => c.Id == model.Currency).First()
});
_auctionContext.SaveChanges();
我认为我有这个
<section>
@Html.LabelFor(model => model.Currencies)
<div>
@Html.DropDownList("Currency", Model.Currencies, "Select Currency", new { required = "required" })
@Html.ValidationMessageFor(model => model.Currencies)
</div>
</section>
上面提到的我的viewmodel看起来像这样
public class AuctionEventDetailsModel
{
public AuctionEventDetailsModel()
{
Currencies = new List<SelectListItem>();
}
public string Title { get; set; }
public string Description { get; set; }
public DateTime Starts { get; set; }
public DateTime Ends { get; set; }
public decimal DepositAmount { get; set; }
public List<SelectListItem> Currencies { get; set; }
public int Currency { get; set; }
}
由于某些原因,当控制器中的代码执行时,它总是在数据库中Currency_Id
表的AuctionEvents
字段中放置一个空值。这是为什么?