MVC 一对零插入不更新

发布于 2024-08-17 00:29:22 字数 1848 浏览 4 评论 0原文

我正在使用实体框架。我有一个“Application”对象(它有一个字段certificateReasonID),它可以有一个或零个CertificateReasons - 因此与“CertificateReason”表有关系,并且在edmx图上直观地我们看不到certificateReasonTypeID字段。

当我更新应用程序时 - 它插入新的CertificateReason并将Application.certificateReasonTypeID更新为新ID - 而不是将Application.certificateReasonTypeID更新为所选ID。

CertificateReason 对象处于添加状态(技术上正确)。 aspx代码是

<%foreach (var certReason in Model.CertificateReasons)                  
                  { %>
                        <li>
                        <%= Html.RadioButton("CertificateReason.id", certReason.id)%>
                        <!-- only because it is adding when it shouldn't -we have to set the other non null values(i.e. not id) in the object else it will fail when it tries to save-->
                          <input type="hidden" value="<%= certReason.meaning %>" name="CertificateReason.meaning"/>
                            <input type="hidden" value="<%= certReason.effectiveFrom %>" name="CertificateReason.effectiveFrom"/>
                            <input type="hidden" value="<%= certReason.createdWhen %>" name="CertificateReason.createdWhen"/>
                        <label for="certificateReasonTypeID<%=certReason.meaning%>"><%=certReason.meaning%></label>
                        </li>
                <%}%>

更新代码是

 public ActionResult Edit(int id, FormCollection collection, ApplicationViewModel model)
 {
     var appToUpdate = OMF.Application.First(m => m.id == id);
     UpdateModel(movieToUpdate, collection.ToValueProvider()); 
     if (ModelState.IsValid)
     {

          OMF.ApplyPropertyChanges(movieToUpdate.EntityKey.EntitySetName, appToUpdate );
          OMF.SaveChanges();
      }
}

谢谢

I am using EntityFramework. I have an "Application" object (which has a field certificateReasonID) which can have one or zero CertificateReasons - so there is a ralationship to the "CertificateReason" table, and visiually on the edmx diagram we do not see the certificateReasonTypeID field.

When I update an Application - it INSERTS a new CertificateReason and UPDATES Application.certificateReasonTypeID to the new ID - instead of updatig Application.certificateReasonTypeID to the selected ID.

The CertificateReason object is in an added state(technically correct).
The aspx code is

<%foreach (var certReason in Model.CertificateReasons)                  
                  { %>
                        <li>
                        <%= Html.RadioButton("CertificateReason.id", certReason.id)%>
                        <!-- only because it is adding when it shouldn't -we have to set the other non null values(i.e. not id) in the object else it will fail when it tries to save-->
                          <input type="hidden" value="<%= certReason.meaning %>" name="CertificateReason.meaning"/>
                            <input type="hidden" value="<%= certReason.effectiveFrom %>" name="CertificateReason.effectiveFrom"/>
                            <input type="hidden" value="<%= certReason.createdWhen %>" name="CertificateReason.createdWhen"/>
                        <label for="certificateReasonTypeID<%=certReason.meaning%>"><%=certReason.meaning%></label>
                        </li>
                <%}%>

The update code is

 public ActionResult Edit(int id, FormCollection collection, ApplicationViewModel model)
 {
     var appToUpdate = OMF.Application.First(m => m.id == id);
     UpdateModel(movieToUpdate, collection.ToValueProvider()); 
     if (ModelState.IsValid)
     {

          OMF.ApplyPropertyChanges(movieToUpdate.EntityKey.EntitySetName, appToUpdate );
          OMF.SaveChanges();
      }
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸 2024-08-24 00:29:22

答案是将应用程序模型传递到编辑帖子中 - 必须在表单上放置一个隐藏字段才能正确填充对象)并确保我获得应用程序对象而不是变量。无法使用 ModelState.IsValid ,因为始终为 false,但正确更新而不是插入。其他一点是必须在获取时执行 .Include("CertificateReasons") 。

public ActionResult Edit(应用程序应用程序,FormCollection集合)
{

        Application movieToEdit = OMF.GetApplication(app.id);           
        if(app.CertificateReason != null)
            movieToEdit.CertificateReason = OMF.CertificateReason.First(d => d.id == app.CertificateReason.id);
        TryUpdateModel(movieToEdit);

        //if (ModelState.IsValid) --can't use this
       // {
            OMF.SaveChanges();
       // }
        return RedirectToAction("Edit", app.id);

    }

answer was to pass the Application model into the Edit post - had to put a hidden field on form for the object to be filled correctly) and ensure I was getting the Application objetc not a var. Can't use the ModelState.IsValid as always false, but correctly updates not inserts. Other point was had to do an .Include("CertificateReasons") on the get.

public ActionResult Edit(Application app, FormCollection collection)
{

        Application movieToEdit = OMF.GetApplication(app.id);           
        if(app.CertificateReason != null)
            movieToEdit.CertificateReason = OMF.CertificateReason.First(d => d.id == app.CertificateReason.id);
        TryUpdateModel(movieToEdit);

        //if (ModelState.IsValid) --can't use this
       // {
            OMF.SaveChanges();
       // }
        return RedirectToAction("Edit", app.id);

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文