これは、mvc3、Razorビューエンジン、vb.netプロジェクトです。私はある場所で立ち往生していて、それは多かれ少なかれ今までこのようなことをする必要がないという問題です。
基本的に問題の要点は、テーブルにリストされているアイテムの量に応じて、1つから多数のチェックボックスを含めることができるビューがあることです。チェックボックスはすべて、そのテーブルから提供された名前も使用します。ユーザーがチェックしたチェックボックスはすべて、モデルが返されるときにコントローラーに保存されます。
送信する場合を除いて、すべてが正しく機能しています。送信ボタンをクリックすると、モデルのボディプロパティがすべて失われます。
@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変数にウォッチを配置して、本体が空であることを確認できるため、以下以外のものは必要ありません。
<AcceptVerbs(HttpVerbs.Post)>
Function EditCourse(ByVal _eCourse As CourseModel) As ActionResult
'Do something with _eCourse
Return RedirectToAction("Blah")
End Function
モデルが提出されたときに私がBodiesプロパティを失う理由についてのアイデアはありますか?