【问题标题】:Edit model including navigation properties MVC4编辑模型,包括导航属性 MVC4
【发布时间】:2012-11-22 23:17:29
【问题描述】:

我有一个人模型:

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 Account
Public Property Id As Long
Public Property PersonId As Long
Public Property Balance As Decimal
Public Property SchemaId As Long
Public Property HasAutoRefill As Boolean

Public Overridable Property Schema As AccountSchema
Public Overridable Property Person As Person

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

这是我的 Person/EditorTemplates/Account:

@ModelType IDCompany.WEB.Account

    @Html.EditorFor(Function(model) model.PersonId)
    @Html.EditorFor(Function(model) model.Id)
    @Html.EditorFor(Function(model) model.HasAutoRefill)
    @Html.EditorFor(Function(model) model.SchemaId)

还有我的 Person/EditorTemplates/PaymentCard:

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

这是我的 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

我是 MVC 的新手。 当我尝试在视图中编辑 som 数据时,出现错误: “发生了参照完整性约束违规:定义参照约束的属性值在关系中的主体和从属对象之间不一致。”关于“db.Entry(pc).State = EntityState.Modified”

我做错了什么?

【问题讨论】:

    标签: asp.net-mvc vb.net navigation


    【解决方案1】:

    您的代码中有一些问题:

    1. 你为什么要多次调用db.SaveChanges()?最后你应该只调用一次。全部或全部保存。
    2. 当模型绑定器为您的操作创建参数对象时,它对 EF 一无所知。例如,PaymentCard 对象的 Person 属性将不会被设置。当您尝试通过调用db.Entry(pc).State = EntityState.Modified 将对象重新附加到数据上下文时,这可能是 EF 抱怨的地方。您应该通过 id 从数据库中重新查询对象并映射所需/更改的属性。最好不要将 EF 类直接传递给视图,而是使用普通的 ViewModel 类。
    3. 您将EditorFor 用于Person.PaymentCards 类型的属性ICollection(Of PaymentCard)。您的 EditorTemplate 的模型类型为 PaymentCard。那是行不通的。您可以为 ICollection(Of PaymentCard) 编写另一个 EditorTemplate,或者只是在您的视图中进行迭代(抱歉,在 C# 中):

      @foreach(var acc in Model.Accounts) {
          @Html.EditorFor(m => acc)
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多