0

这是一个 mvc3,Razor 视图引擎,vb.net 项目。我被困在一个地方,这或多或少是一个问题,直到现在才不得不做这样的事情。

基本上,问题的关键在于我有一个视图,其中可以包含 1 到多个复选框,具体取决于表中列出的项目数量。复选框也将使用该表提供的名称。当模型被传回时,用户选中的任何复选框都将保存在控制器中。

除非提交,否则一切正常。单击提交按钮时,我失去了模型的 Bodies 属性。

@ModelType xxxxxx.CourseModel

@Code
ViewData("Title") = "Edit Courses"
End Code

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"  type="text/javascript"></script>


 @Using Html.BeginForm(Nothing, Nothing, FormMethod.Post, New With {.enctype = "multipart/form-data"})
 @Html.ValidationSummary(True)

    @<fieldset>
    <legend>Edit Courses</legend>
    @Html.HiddenFor(Function(model) model.cId)
    <table style="float: left">
    <tr>

     <th>Certification Bodies</th>
     </tr>
     <tr>
     @For _i As Integer = 0 To Model.Bodies.Count - 1
            Dim i = _i
            @<td>@Html.CheckBoxFor(Function(model) model.Bodies.ElementAt(i).certSelected)@Html.DisplayFor(Function(f) f.Bodies.ElementAt(i).certName)@Html.HiddenFor(Function(model) model.Bodies.ElementAt(i).certBodyId)</td>
        Next

    </tr>
    <tr><th><input type="submit" value="Save" /></th></tr>
    </table>

目前模型如下:

    Public Class CourseModel
       Private _cId As Integer
       Public Property cId() As Integer
       Get
        Return _cId
       End Get
       Set(ByVal value As Integer)
        _cId = value
       End Set
       End Property
       Private m_Bodies As New List(Of CertBodyVM)
    Public Property Bodies() As List(Of CertBodyVM)
    Get
        Return m_Bodies
    End Get
    Set(value As List(Of CertBodyVM))
        m_Bodies = value
    End Set
    End Property
    End Class

这是 CertBodyVM

Public Class CertBodyVM
Private _certName As String
Public Property certName() As String
    Get
        Return _certName
    End Get
    Set(ByVal value As String)
        _certName = value
    End Set
End Property
Private _certSelected As Boolean
Public Property certSelected() As Boolean
    Get
        Return _certSelected
    End Get
    Set(ByVal value As Boolean)
        _certSelected = value
    End Set
End Property
Private m_certBodyId As Integer
Public Property certBodyId() As Integer
    Get
        Return m_certBodyId
    End Get
    Set(ByVal value As Integer)
        m_certBodyId = value
    End Set
End Property
End Class

最后是加载此视图的控制器功能..

  Function EditCourse(ByVal id As Integer) As ViewResult

        Dim courses As New CourseModel
        Dim newCourse As cours = db.courses.Single(Function(c) c.course_id = id)

        courses.cId = newCourse.course_id

        courses.cRef = newCourse.course_ref

        Dim cBodies As List(Of certbody) = db.certbodies.Where(Function(F) F.confNum = _AnnualNumber AndAlso F.Display = 1).ToList
        For Each _cb In cBodies
            Dim cb = _cb
            Dim x As New CertBodyVM
            x.certBodyId = cb.idCertBodies
            x.certName = cb.CertBodyName
            Try
                Dim csetBodies As coursecertifybody = db.coursecertifybodies.Where(Function(f) f.cert_Body_id = cb.idCertBodies AndAlso f.course_ref = newCourse.course_ref AndAlso f.course_id = newCourse.course_id).FirstOrDefault
                If Not IsNothing(csetBodies) Then
                    x.certSelected = True
                Else
                    x.certSelected = False
                End If
            Catch ex As Exception
                x.certSelected = False
            End Try
            courses.Bodies.Add(x)
        Next

        Return View(courses)
    End Function

为了符合 SO 标准,我在下面包含了后控制器功能,因为我可以在 _eCourse var 上放置一个手表并看到身体是空的,所以除了下面的内容之外不需要任何东西。

   <AcceptVerbs(HttpVerbs.Post)>
    Function EditCourse(ByVal _eCourse As CourseModel) As ActionResult
     'Do something with _eCourse
    Return RedirectToAction("Blah")
    End Function

任何想法为什么我在提交模型时丢失了 Bodies 属性???

4

1 回答 1

1

集合的模型绑定不适用于.elementAt(i). 相反,尝试使用这个:

@Html.CheckBoxFor(Function(model) model.Bodies[i].certSelected)
@Html.DisplayFor(Function(f) f.Bodies[i].certName)
@Html.HiddenFor(Function(model) model.Bodies[i].certBodyId)
于 2013-03-03T22:34:18.550 回答