【发布时间】:2016-07-04 08:48:10
【问题描述】:
模型 这些是模型类,我正在使用实体框架。我正在使用这些模型来实现级联下拉列表。
public class League
{
public int Id { get; set; }
public string League1 { get; set; }
public string Icon { get; set; }
public virtual ICollection<LeagueDivision> LeagueDivisions { get; set; }
}
public class LeagueDivision
{
public int Id { get; set; }
public Nullable<int> LeagueId { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public virtual League League { get; set; }
}
public partial class CalculatorPrice
{
public int Id { get; set; }
public int LeagueId { get; set; }
public int LeagueDivisionId { get; set; }
public Nullable<decimal> Price { get; set; }
}
public class ViewModelForHostBooster
{
[Required(ErrorMessage = "Please enter price")]
[Display(Name = "Price")]
public decimal Price { get; set; }
[Required(ErrorMessage = "Please select a league")]
[Display(Name = "League")]
public int? SelectedLeague { get; set; }
[Required(ErrorMessage = "Please select a league division")]
[Display(Name = "League Division")]
public int? SelectedLeagueDivision { get; set; }
public SelectList LeagueList { get; set; }
public SelectList LeagueDivisionList { get; set; }
}
控制器/动作 在 HttpGet 中,我刚刚填充了级联下拉列表并且工作正常,现在我正在为此实现 Httppost。我想根据从下拉列表中选择的列表项存储 price,如果 price 已经存在,那么我想更新它。第一次我可以成功添加 price 但第二次尝试更新它时我得到 System.Data.Entity.Infrastructure.DbUpdateConcurrencyException 请任何人指导我如何处理这个。
[HttpPost]
public ActionResult IndexDropDown(ViewModelForHostBooster model)
{
if (!ModelState.IsValid)
{
ConfigureViewModel(model);
return View(model);
}
else
{
HostBoostersDBEntities2 db = new HostBoostersDBEntities2();
CalculatorPrice calculatePrice = new CalculatorPrice();
var calculatePriceExistsOrNot = db.CalculatorPrices
.Where(x => x.LeagueId == model.SelectedLeague
&&
x.LeagueDivisionId == model.SelectedLeagueDivision).ToList();
if (calculatePriceExistsOrNot.Count > 0)
{
calculatePrice.LeagueId = (int)model.SelectedLeague;
calculatePrice.LeagueDivisionId = (int)model.SelectedLeagueDivision;
calculatePrice.Price = model.Price;
db.Entry(calculatePrice).State = EntityState.Modified;
异常发生在 db.SaveChanges(); 行 “System.Data.Entity.Infrastructure.DbUpdateConcurrencyException”发生在 EntityFramework.dll 中,但未在用户代码中处理。 附加信息:存储更新、插入或删除语句影响了意外数量的行 (0)。自加载实体后,实体可能已被修改或删除。刷新 ObjectStateManager 条目。
db.SaveChanges();
}
else
{
calculatePrice.LeagueId = (int)model.SelectedLeague;
calculatePrice.LeagueDivisionId = (int)model.SelectedLeagueDivision;
calculatePrice.Price = model.Price;
db.CalculatorPrices.Add(calculatePrice);
db.SaveChanges();
}
}
ConfigureViewModel(model);
return View(model);
}
查看
@using (Html.BeginForm("IndexDropDown", "DropDown", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<div>
@Html.LabelFor(m => m.Price, new { @class ="control-lebel"})
@Html.TextBoxFor(m => m.Price, new { @class = "form-control"})
@Html.ValidationMessageFor(m => m.Price)
</div>
<div>
@Html.LabelFor(m => m.SelectedLeague ,new { @class ="control-lebel"})
@Html.DropDownListFor(m => m.SelectedLeague, Model.LeagueList, new { @class = "form-control"})
@Html.ValidationMessageFor(m => m.SelectedLeague)
</div>
<div>
@Html.LabelFor(m => m.SelectedLeagueDivision ,new { @class ="control-lebel"})
@Html.DropDownListFor(m => m.SelectedLeagueDivision, Model.LeagueDivisionList, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.SelectedLeagueDivision)
</div>
<input type="submit" value="save" />
}
【问题讨论】:
-
@Stephen Muecke 在互联网上搜索此问题的解决方案后,我了解到如果我们想要更新任何实体,我们不需要创建该实体的新对象。我们只需要将它传递给 db.Entry(model).State = EntityState.Modified;但我正在创建calculatorPrice 的新实例。问题在这里,我在参数中传递 ViewModelForHostBoosters 并想要更新calculatorPrice 表。你能指导我如何管理我的场景吗?
标签: c# asp.net-mvc entity-framework