ASP.NET MVC - 外键/父子和编辑记录
我有许多类,它们与其他类的属性(如位置、货币等)有关系。采用以下示例:
Public Class Transaction
Public Property ID As Integer
Public Property Description As String
Public Property Quantity As Integer
Public Property SaleAmount As Double
Public Overridable Property Currency As Currency
End Class
Public Class Currency
Public Property ID As String
Public Property Description As String
Public Property Symbol As String
Public Property SymbolImage As String
End Class
当我初始化应用程序以供首次使用时,我添加了我的货币。添加交易时,我有一个下拉框来选择货币。 我将交易保存到数据库没有任何问题,并且货币 ID 也被保存。
当我编辑交易并尝试更改货币时,我无法将其保存回数据库。
<HttpPost()>
Function Edit(transaction As Transaction) As ActionResult
transaction.Currency = db.Currencies.Find(transaction.Currency.ID)
Debug.Print("Currency: " & transaction.Currency.ID)
If ModelState.IsValid Then
db.Entry(transaction).State = EntityState.Modified
db.SaveChanges()
Return RedirectToAction("Index")
End If
Return View(transaction)
End Function
当我在上面的 post 方法中执行 debug.print 时,货币被正确报告为更改后的货币,但数据库中交易记录上的货币 ID 未更新。
我已经做了一些搜索和阅读,但没有找到太多/任何东西。 我确实尝试将这一行添加到 post 方法中,但它仍然没有保存更改:
db.Entry(transaction.Currency).State = EntityState.Modified
我很困惑,希望得到任何帮助!
I've got a number of classes which have a relationship to other classes for properties like Location, Currency etc. Take the following example:
Public Class Transaction
Public Property ID As Integer
Public Property Description As String
Public Property Quantity As Integer
Public Property SaleAmount As Double
Public Overridable Property Currency As Currency
End Class
Public Class Currency
Public Property ID As String
Public Property Description As String
Public Property Symbol As String
Public Property SymbolImage As String
End Class
I add my currencies when I initialise the application for first use. When adding a transaction, I have a drop down box to select the currency.
I have no issues saving the Transaction to the db and the currency ID is saved also.
When I edit the transaction and try to change the currency, I can't get it to save back to the db.
<HttpPost()>
Function Edit(transaction As Transaction) As ActionResult
transaction.Currency = db.Currencies.Find(transaction.Currency.ID)
Debug.Print("Currency: " & transaction.Currency.ID)
If ModelState.IsValid Then
db.Entry(transaction).State = EntityState.Modified
db.SaveChanges()
Return RedirectToAction("Index")
End If
Return View(transaction)
End Function
When I do the debug.print in the post method above, the currency is correctly being reported as the changed currency but the Currency ID on the Transaction record in the DB isn't updated.
I've done some searching and reading and haven't found much/anything.
I did try adding this line to the post method but it still didn't save the changes:
db.Entry(transaction.Currency).State = EntityState.Modified
I'm stumped and would appreciate any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以这是我能找到的最佳解决方案。我有兴趣听到其他方法来达到相同的结果。我发现的其他方法比这个复杂得多。
我已经设置了编辑视图,其中包含一个包含货币列表的下拉列表和一个用于存储CurrencyID 的隐藏字段。编辑帖子如下所示:
So here's the best solution I could find. I'd be interested to hear other ways to achieve the same result. The other methods I've found are much more complicated than this.
I've setup the Edit view with a drop down list containing a list of the currencies and a hidden field to store the CurrencyID. The edit post looks like this: