Asp.net mvc 编辑模型,包括导航属性MVC4
我有一个人模型:Asp.net mvc 编辑模型,包括导航属性MVC4,asp.net-mvc,vb.net,navigation,Asp.net Mvc,Vb.net,Navigation,我有一个人模型: Partial Public Class Person Public Property Id As Long <DisplayName("Fornavn")> Public Property Firstname As String <DisplayName("Efternavn")> Public Property Lastname As String Public Property chr_cpr As String <DisplayName("
Partial Public Class Person
Public Property Id As Long
<DisplayName("Fornavn")>
Public Property Firstname As String
<DisplayName("Efternavn")>
Public Property Lastname As String
Public Property chr_cpr As String
<DisplayName("Medarbejdernummer")>
Public Property EmployeeNumber As String
Public Overridable Property Accounts As ICollection(Of Account) = New HashSet(Of Account)
Public Overridable Property PaymentCards As ICollection(Of PaymentCard) = New HashSet(Of PaymentCard)
Public Overridable Property PaymentRoles As ICollection(Of PaymentRole) = New HashSet(Of PaymentRole)
Public Overridable Property AllPaymentRoles As ICollection(Of PaymentRole) = New HashSet(Of PaymentRole)
Public Overridable Property BillableDepartments As ICollection(Of Department) = New HashSet(Of Department)
Public Overridable Property AllDepartments As ICollection(Of Department) = New HashSet(Of Department)
Public Overridable Property AccessablePosDevices As ICollection(Of PosDevice) = New HashSet(Of PosDevice)
Public Overridable Property AllPosDevices As ICollection(Of PosDevice) = New HashSet(Of PosDevice)
End Class
以及支付卡模式:
Partial Public Class PaymentCard
Public Property Id As Long
Public Property Serial As String
Public Property TypeId As Nullable(Of Long)
Public Property IsActive As Boolean
Public Property PersonId As Long
Public Overridable Property Type As CardType
Public Overridable Property Person As Person
End Class
我希望能够从Person/edit视图编辑Person、PaymentCard和Account属性。我用的是EditorTemplates。我的看法是:
@ModelType IDCompany.WEB.Person
@Using Html.BeginForm()
@Html.ValidationSummary(True)
@<fieldset>
<legend>Person</legend>
@Html.HiddenFor(Function(model) model.Id)
<div class="editor-label">
@Html.LabelFor(Function(model) model.Firstname)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.Firstname)
@Html.ValidationMessageFor(Function(model) model.Firstname)
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.Lastname)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.Lastname)
@Html.ValidationMessageFor(Function(model) model.Lastname)
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.chr_cpr)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.chr_cpr)
@Html.ValidationMessageFor(Function(model) model.chr_cpr)
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.EmployeeNumber)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.EmployeeNumber)
@Html.ValidationMessageFor(Function(model) model.EmployeeNumber)
</div>
</fieldset>
@<table class="cardTable">
<tr>
<th>Id</th>
<th>Serienummer</th>
<th>Type</th>
<th>Aktiv</th>
</tr>
@Html.EditorFor(Function(x) x.PaymentCards)
</table>
@Html.EditorFor(Function(x) x.Accounts)
@<p><input type="submit" value="Save" /></p>
End Using
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@Section Scripts
@Scripts.Render("~/bundles/jqueryval")
End Section
我的个人/编辑模板/支付卡:
@ModelType IDCompany.WEB.PaymentCard
@Html.HiddenFor(Function(model) model.TypeId)
@Html.HiddenFor(Function(model) model.PersonId)
@Html.HiddenFor(Function(model) model.Id)
@Html.HiddenFor(Function(model) Model.Serial)
@Html.HiddenFor(Function(model) Model.Type.Name)
<tr class="cardRow">
<td>@Model.Id</td>
<td>@Model.Serial</td>
<td>@Model.Type.Name</td>
<td>@Html.EditorFor(Function(model) model.IsActive)</td>
</tr>
@ModelType IDCompany.WEB.PaymentCard
@Html.HiddenFor(函数(模型)model.TypeId)
@Html.HiddenFor(函数(模型)model.PersonId)
@Html.HiddenFor(函数(模型)model.Id)
@Html.HiddenFor(函数(模型)model.Serial)
@Html.HiddenFor(函数(模型)model.Type.Name)
@Model.Id
@型号.序列号
@Model.Type.Name
@EditorFor(函数(模型)model.IsActive)
这是我的PersonController中的编辑功能:
<HttpPost()> _
Function Edit(ByVal person As Person) As ActionResult
Dim kontoSubMenu As New List(Of MenuPoint)
Dim emptyList As New List(Of String)
kontoSubMenu.Add(New MenuPoint("Kantine", emptyList, "Kontooplysninger", "Home", "All"))
kontoSubMenu.Add(New MenuPoint("Personer", emptyList, "Personer", "Person", "All"))
kontoSubMenu.Add(New MenuPoint("Test", emptyList, "LogUd", "Home", "All"))
ViewBag.kontoSubMenu = kontoSubMenu
If ModelState.IsValid Then
Dim paymentcardList
paymentcardList = person.PaymentCards.ToList()
For Each pc In paymentcardList
db.Entry(pc).State = EntityState.Modified
db.SaveChanges()
Next
Dim accountList
accountList = person.Accounts.ToList()
For Each ac As Account In accountList
db.Entry(ac).State = EntityState.Modified
'Dim accountValue As Account = db.Accounts.Find(accountt.Id)
db.SaveChanges()
Next
db.Entry(person).State = EntityState.Modified
db.SaveChanges()
Return RedirectToAction("Index")
End If
Return View(person)
End Function
_
函数编辑(ByVal person As person)作为操作结果
Dim Konto子菜单作为新列表(菜单点)
Dim emptyList作为新列表(字符串)
添加(新菜单点(“Kantine”,空列表,“Kontooplysninger”,“Home”,“All”))
添加(新菜单点(“Personer”,空列表,“Personer”,“Person”,“All”))
添加(新的菜单点(“Test”、空列表、“LogUd”、“Home”、“All”))
ViewBag.kontoSubMenu=kontoSubMenu
如果ModelState.IsValid,则
Dim paymentcardList
paymentcardList=person.PaymentCards.ToList()
对于paymentcardList中的每台电脑
db.Entry(pc.State=EntityState.Modified
db.SaveChanges()
下一个
Dim帐户列表
accountList=person.Accounts.ToList()
对于accountList中的每个ac As帐户
db.Entry(ac.State=EntityState.Modified
'Dim accountValue As Account=db.Accounts.Find(accountt.Id)
db.SaveChanges()
下一个
db.Entry(person.State=EntityState.Modified
db.SaveChanges()
返回重定向操作(“索引”)
如果结束
返回视图(个人)
端函数
我是MVC的新手。
当我试图在视图中编辑som数据时,会出现以下错误:
发生引用完整性约束冲突:定义引用约束的属性值在关系中的主体对象和从属对象之间不一致。“on”db.Entry(pc.State=EntityState.Modified
我做错了什么 您的代码中有一些问题:
db.SaveChanges()
?最后你应该只叫它一次。要么全部保存,要么什么都不保存PaymentCard
对象的Person
属性将无法设置。当您试图通过调用db.Entry(pc.State=EntityState.Modified
将对象重新附加到datacontext时,这可能就是EF所抱怨的问题。您应该从数据库中按id重新查询对象,并映射所需/更改的属性。最好不要将EF类直接传递给视图,而是使用普通的ViewModel类EditorFor
编辑属性Person.PaymentCards
,该属性的类型为ICollection(PaymentCard)
。您的EditorTemplate的型号为PaymentCard
。那不行。您可以为(PaymentCard的)ICollection编写另一个编辑器或模板,或者只是在视图中迭代(抱歉,在C中):
@ModelType IDCompany.WEB.PaymentCard
@Html.HiddenFor(Function(model) model.TypeId)
@Html.HiddenFor(Function(model) model.PersonId)
@Html.HiddenFor(Function(model) model.Id)
@Html.HiddenFor(Function(model) Model.Serial)
@Html.HiddenFor(Function(model) Model.Type.Name)
<tr class="cardRow">
<td>@Model.Id</td>
<td>@Model.Serial</td>
<td>@Model.Type.Name</td>
<td>@Html.EditorFor(Function(model) model.IsActive)</td>
</tr>
<HttpPost()> _
Function Edit(ByVal person As Person) As ActionResult
Dim kontoSubMenu As New List(Of MenuPoint)
Dim emptyList As New List(Of String)
kontoSubMenu.Add(New MenuPoint("Kantine", emptyList, "Kontooplysninger", "Home", "All"))
kontoSubMenu.Add(New MenuPoint("Personer", emptyList, "Personer", "Person", "All"))
kontoSubMenu.Add(New MenuPoint("Test", emptyList, "LogUd", "Home", "All"))
ViewBag.kontoSubMenu = kontoSubMenu
If ModelState.IsValid Then
Dim paymentcardList
paymentcardList = person.PaymentCards.ToList()
For Each pc In paymentcardList
db.Entry(pc).State = EntityState.Modified
db.SaveChanges()
Next
Dim accountList
accountList = person.Accounts.ToList()
For Each ac As Account In accountList
db.Entry(ac).State = EntityState.Modified
'Dim accountValue As Account = db.Accounts.Find(accountt.Id)
db.SaveChanges()
Next
db.Entry(person).State = EntityState.Modified
db.SaveChanges()
Return RedirectToAction("Index")
End If
Return View(person)
End Function
@foreach(var acc in Model.Accounts) {
@Html.EditorFor(m => acc)
}