我已经两次遇到这个问题了,我不太明白为什么。我通过发回视图模型在我的数据库中创建葡萄酒。该视图模型的 Wine 属性已正确填充,但是当我将更改保存在数据库中时,导航属性不会填充 - 就像我想的那样。我需要这个,因为我将这个 wine 对象传递给一个方法来将这些导航属性添加到我的搜索索引中。
我试图将数据库重新查询到一个新变量,但这也不起作用。我对此感到非常困惑-所以任何帮助都会很棒。我遇到了一个非常相似的问题,在这里没有收到任何答案。其他人通过在这里重新查询数据库解决了这个问题,但这对我不起作用。该模型在 GET 请求上很好地填充,但不是在帖子上。
控制器:
[HttpPost]
//[Authorize(Roles = "admin, producereditor")]
public ActionResult Create(NewWineViewModel nw)
{
if (ModelState.IsValid)
{
nw.Wine.Active = nw.IsRequest ? false : true;
//keep nullable for requests.
nw.Wine.ImporterID = nw.Wine.ImporterID == 0 ? null : nw.Wine.ImporterID;
nw.Wine.VarTypeID = nw.Wine.VarTypeID == 0 ? null : nw.Wine.VarTypeID;
nw.Wine.OriginID = nw.Wine.OriginID == 0 ? null : nw.Wine.OriginID;
nw.Wine.AppID = nw.Wine.AppID == 0 ? null : nw.Wine.AppID;
nw.Wine.VintageID = nw.Wine.VintageID == 0 ? null : nw.Wine.VintageID;
nw.Wine.CreatedBy = this.User.Identity.Name;
nw.Wine.CreatedOn = DateTime.Now;
db.Wines.Add(nw.Wine);
db.SaveChanges();
var wineToIndex = db.Wines.Find(nw.Wine.WineID);
// nw.Wine.QRUrl = WineUtils.MakeQRCode(nw.Wine);
//db.SaveChanges();
//Lucene.LuceneSearch.AddUpdateLuceneIndex(db.Wines.Find(nw.Wine.WineID));
if (nw.IsRequest)
{
nw.VOAVIRequest.WineID = nw.Wine.WineID;
nw.VOAVIRequest.CreatedBy = User.Identity.Name;
nw.VOAVIRequest.CreatedOn = DateTime.Now;
db.VOAVIRequests.Add(nw.VOAVIRequest);
db.SaveChanges();
return RedirectToAction("Requested");
//redirect to "Request Submitted" page for new wines
}
return RedirectToAction("Details", new { id = nw.Wine.WineID });
}
ViewBag.VarTypeID = new SelectList(db.VarTypes, "VarTypeID", "Name").Default("Select a Varietal/Type", nw.Wine.VarTypeID.ToString());
ViewBag.OriginID = new SelectList(db.Origins, "OriginID", "Name").Default("Select an Origin", nw.Wine.OriginID.ToString());
ViewBag.AppID = new SelectList(db.Apps, "AppID", "Name").Default("Select an Appellation", nw.Wine.AppID.ToString());
ViewBag.VintageID = new SelectList(db.Vintages, "VintageID", "Name").Default("Select a Vintage", nw.Wine.VintageID.ToString());
ViewBag.ImporterID = new SelectList(db.Importers, "ImporterID", "Name").Default("Select an Importer", nw.Wine.ImporterID.ToString());
if (User.IsInRole("producer"))
{
Producer currentProd = db.ProducerUsers.Find(Membership.GetUser().ProviderUserKey).Producer;
ViewBag.ProducerID = currentProd.ProducerID;
ViewBag.ProducerName = currentProd.Name;
}
else
{
ViewBag.ProducerSelect = new SelectList(db.Producers, "ProducerID", "Name", nw.Wine.ProducerID);
}
return View(nw);
}
视图模型:
public class NewWineViewModel
{
public Wine Wine { get; set; }
public VOAVIRequest VOAVIRequest { get; set; }
public bool IsRequest { get; set; }
public SelectList VarTypes { get; set; }
public SelectList Origins { get; set; }
public SelectList Apps { get; set; }
public SelectList Vintages { get; set; }
public SelectList Importers { get; set; }
public NewWineViewModel()
{
this.Wine = new Wine();
}
}
模型:
public class Wine :Updater
{
public int WineID { get; set; }
//public int WineTypeID { get; set; }
[Display(Name = "Varietal/Type")]
public int? VarTypeID { get; set; }
[Display(Name = "Origin")]
public int? OriginID { get; set; }
[Display(Name = "Appellation")]
public int? AppID { get; set; }
[Display(Name = "Vintage")]
public int? VintageID { get; set; }
[Display(Name = "Importer")]
public int? ImporterID { get; set; }
public int ProducerID { get; set; }
public string Designate { get; set; }
[Display(Name = "Drink Window")]
public string DrinkWindow { get; set; }
public string Body { get; set; }
public string SKU { get; set; }
[Display(Name = "Varietal Makeup")]
public string VarietalMakeup { get; set; }
[Display(Name = "Case Production")]
public string CaseProduction { get; set; }
[Display(Name = "Alcohol Content")]
public double? AlcoholContent { get; set; }
public string Winemaker { get; set; }
[Display(Name = "Consulting Winemaker")]
public string ConsultWinemaker { get; set; }
public bool Sustainable { get; set; }
public bool Kosher { get; set; }
public bool Organic { get; set; }
public bool Biodynamic { get; set; }
public bool SalmonSafe { get; set; }
public Boolean Active { get; set; }
[Display(Name = "ResidualSugar")]
public double? RS { get; set; }
public double? pH { get; set; }
public string QRUrl { get; set; }
public virtual WineType WineType { get; set; }
public virtual VarType VarType { get; set; }
public virtual Origin Origin { get; set; }
public virtual App App { get; set; }
public virtual Vintage Vintage { get; set; }
public virtual Importer Importer { get; set; }
public virtual Producer Producer { get; set; }
[JsonIgnore]
public virtual ICollection<POS> POSs { get; set; }
[JsonIgnore]
public virtual ICollection<Review> Reviews { get; set; }
[JsonIgnore]
public virtual ICollection<Doc> Docs { get; set; }
[JsonIgnore]
public IEnumerable<SelectListItem> BodyList { get; set; }