加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用TryUpdateModel方法在ASP.NET MVC 3中使用EntityFrame

发布时间:2020-12-15 21:34:34 所属栏目:百科 来源:网络整理
导读:在尝试通过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
在尝试通过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 int GoalsGuest { get; set; }

       public virtual Fixture { get; set; }

    }

在控制器中,我通过where语句过滤表,只获得与用户匹配的entrys:

[HttpGet]
    public ActionResult Index()
    {
        var user = User.Identity.Name;
        var model = _db.Bets.Where(t => t.Better == user);

        return View(model);
    }

View是两部分,一部分负责表头,另一部分列出用户的所有赌注:

<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>

EditorTemplate:

@model Betting.Models.Bet
<tr>
<td>
    @Html.DisplayFor(modelItem => Model.Fixture.Kickoff)
</td>
<td>
    @Html.DisplayFor(modelItem => Model.Fixture.HomeTeam)
</td>
<td>
    @Html.DisplayFor(modelItem => Model.Fixture.GuestTeam)
</td>
<td>
    @Html.DisplayFor(modelItem => Model.Fixture.Group)
</td>
<td>
    @Html.TextBoxFor(modelItem => Model.GoalsHome,new { style = "width: 30px" }):
    @Html.TextBoxFor(modelItem => Model.GoalsGuest,new { style = "width: 30px" })
</td>
    @Html.HiddenFor(modelItem => Model.FixtureID)
</tr>

回到控制器,我尝试更新模型:

[HttpPost]
    public ActionResult Index(FormCollection collection)
    {
        var user = User.Identity.Name;
        var model = _db.Bets.Where(t => t.Better == user);
        TryUpdateModel(model);
        _db.SaveChanges();

        return RedirectToAction("Index");
    }

这绝对没有.实体框架根本不会更新数据库.我甚至将表格分开,以便html文件中的结果下注标签可以区分(注意名称值之前的[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" />

有人可以告诉我为什么实体框架没有更新数据库?对象映射有问题吗?

解决方法

我之前遇到过同样的问题. MSDN在这个问题上可能会让人感到困惑,但是页面的中间部分说明如果你创建没有proxycreationenabled的POCO实体,你必须在调用savechanges之前调用detectchanges.

TryUpdateModel(model);
_db.DetectChanges();
_db.SaveChanges();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读