这是一个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属性的任何想法???
答案 0 :(得分: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)