MVC 一对零插入不更新
我正在使用实体框架。我有一个“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是将应用程序模型传递到编辑帖子中 - 必须在表单上放置一个隐藏字段才能正确填充对象)并确保我获得应用程序对象而不是变量。无法使用 ModelState.IsValid ,因为始终为 false,但正确更新而不是插入。其他一点是必须在获取时执行 .Include("CertificateReasons") 。
public ActionResult Edit(应用程序应用程序,FormCollection集合)
{
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)
{