C# 使用TryUpdateModel方法在ASP.NET MVC 3中使用EntityFramework更新多个对象
当我试图通过实体框架更新数据时,我正在努力使用ASP.NET MVC3。过程如下: 我有这个模型:C# 使用TryUpdateModel方法在ASP.NET MVC 3中使用EntityFramework更新多个对象,c#,asp.net-mvc-3,entity-framework,C#,Asp.net Mvc 3,Entity Framework,当我试图通过实体框架更新数据时,我正在努力使用ASP.NET MVC3。过程如下: 我有这个模型: public class Bet { public string BetID { get; set; } public int FixtureID { get; set; } public string Better { get; set; } public int GoalsHome { get; set; }
public class Bet
{
public string BetID { get; set; }
public int FixtureID { get; set; }
public string Better { get; set; }
public int GoalsHome { get; set; }
public int GoalsGuest { get; set; }
public virtual Fixture { get; set; }
}
在控制器中,我通过where语句过滤表,以仅获取与用户匹配的入口:
[HttpGet]
public ActionResult Index()
{
var user = User.Identity.Name;
var model = _db.Bets.Where(t => t.Better == user);
return View(model);
}
<fieldset>
<legend>Bets</legend>
<table>
<tr>
<th>
Kickoff
</th>
<th>
Home Team
</th>
<th>
Guest Team
</th>
<th>
Group
</th>
<th>
Bet
</th>
</tr>
@Html.EditorFor(m => m) //this is resolved in an self made EditorTemplate
</table>
该视图分为两部分,一部分负责表头,另一部分列出用户的所有赌注:
[HttpGet]
public ActionResult Index()
{
var user = User.Identity.Name;
var model = _db.Bets.Where(t => t.Better == user);
return View(model);
}
<fieldset>
<legend>Bets</legend>
<table>
<tr>
<th>
Kickoff
</th>
<th>
Home Team
</th>
<th>
Guest Team
</th>
<th>
Group
</th>
<th>
Bet
</th>
</tr>
@Html.EditorFor(m => m) //this is resolved in an self made EditorTemplate
</table>
这完全没有用。实体框架根本不会更新数据库。我甚至将表格分隔开,以便html文件中的结果bet标记可以区分(注意名称值之前的[index]:
<input data-val="true" data-val-number="The field GoalsHome must be a number." data-val-required="The GoalsHome field is required." name="[1].GoalsHome" style="width: 30px" type="text" value="3" />:
<input data-val="true" data-val-number="The field GoalsGuest must be a number." data-val-required="The GoalsGuest field is required." name="[1].GoalsGuest" style="width: 30px" type="text" value="0" />
:
有人能告诉我为什么实体框架没有更新数据库吗?对象映射有问题吗?我以前也遇到过同样的问题。在这个问题上可能会很困惑,但在页面的一半部分指出,如果您在不启用proxycreationenabled的情况下创建POCO实体,则必须调用detectch在调用savechanges之前进行anges
TryUpdateModel(model);
_db.DetectChanges();
_db.SaveChanges();
好的,看来我找到了一种不用TryUpdateModel方法更新数据库的方法。由于html帖子中发送的项目是可区分的,我可以向控制器方法添加一个参数,该参数保存我从视图中获得的赌注。然后我迭代数据库中的结果,并更新随字段va更改的字段从视图中的赌注价值:
[HttpPost]
public ActionResult Index(FormCollection collection, List<Bet> bets)
{
var user = User.Identity.Name;
var model = _db.Bets.Where(t => t.Better== user);
int i = 0;
foreach (var bet in model)
{
Bet the_Bet= bets.Find(
delegate(Bet _bet)
{
return _bet.BetID == bet.BetID;
});
bet.GoalsHome= the_Bet.GoalsHome;
bet.GoalsGuest= the_Bet.GoalsGuest;
i++;
}
_db.SaveChanges();
return RedirectToAction("Index");
}
[HttpPost]
公共行动结果索引(FormCollection集合、列表赌注)
{
var user=user.Identity.Name;
var模型=_db.Bets.Where(t=>t.Better==user);
int i=0;
foreach(模型中的var下注)
{
下注,下注(
代表(下注)
{
return _bet.BetID==bet.BetID;
});
bet.GoalsHome=bet.GoalsHome;
bet.GoalsGuest=bet.GoalsGuest;
i++;
}
_db.SaveChanges();
返回操作(“索引”);
}
我想知道是否还有办法让TryUpdateModel方法工作。你确定这是EF的错吗?我怀疑TryUpdate模型只在单个对象上运行,而Bets。哪里将返回枚举。你能在调试器中验证模型中的对象从FormCollection获取新值吗?你检查了FormCollection的返回值了吗
TryUpdateModel
?或者检查了ModelState
是否存在任何错误?是否启用了更改跟踪?似乎模型对象没有从FormCollection接收到值。因此问题确实是TryUpdateModel方法。TryUpdateModel是否真的只适用于单个对象?不幸的是,这也没有帮助。正如在上面的注释中,TryUpdateModel方法不起作用,因为它似乎只对单个对象起作用;