私は現在、VS2012 で最初の ASP.MVC4 プロジェクトに取り組んでいます。テキスト ボックスとコンボ ボックス (ブロックの選択) へのデータのバインドをどのように処理し、結果をコントローラーに返すのか疑問に思っていました。
モデル
Public Class OpenBuildFaultsViewModel
Public Property FailureID As Int64
Public Property ModelName As String
Public Property ZoneName As String
Public Property Fault As String
Public Property FaultCode As String
Public Property FaultCodeDetail As String
Public Property FaultArea As String
Public Property MajorAssembly As String
Public Property SubAssembly As String
Public Property ComponentAssembly As String
Public Property BusinessTest As String
Public Property AuditScore As String
Public Property Comment As String
Public Property ShortagePart As String
Public Property CreatedBy As String
Public Property FixedByID As Int32
Public Property FixedByComment As String
Public Property FixedByFaultRectificationID As Int32
End Class
Public Class RectificationCodesViewModel
Friend Property ID As Long
Friend Property Description As String
End Class
コントローラー(失敗サインオフ メソッド)
Namespace QSmartRectification
Public Class FailuresController
Inherits System.Web.Mvc.Controller
Function FailureSignOff() As ActionResult
Dim failureID As Int64 = Convert.ToInt64(TempData("FailureID"))
Dim data As IEnumerable(Of OpenBuildFaultsViewModel) = Session("Failures")
Dim model = data.Where(Function(w) w.FailureID.Equals(failureID))
Dim codes As IEnumerable(Of RectificationCodesViewModel) = TryCast(Session("RectificationCodes"), IEnumerable(Of RectificationCodesViewModel))
If codes IsNot Nothing Then
Dim recCodes = From c In codes
Select New SelectListItem With {.Value = c.ID, .Text = c.Description}
ViewBag.RectificationCodes = recCodes
End If
Return View(model.FirstOrDefault)
End Function
' POST: /Failures/FailureSignOff
<HttpPost> _
Function FailureSignOff(model As OpenBuildFaultsViewModel) As ActionResult
Stop
End Function
End Class
End Namespace
意見
@ModelType QSmartRectification.OpenBuildFaultsViewModel
@Code
ViewData("Title") = "Failure Sign Off"
End Code
@section featured
<section class="featured">
<div class="content-wrapper" >
<p class="site-title float-left">QSmart Rectification Sign Off</p>
<input type="image" src="../../Images/Product.png" />
</div>
</section>
End Section
<br />
@Using Html.BeginForm()
@Html.ValidationSummary()
@<fieldset class="fieldset">
<legend>Fault Sign Off</legend>
<section class="contentdetail">
<section class="block">
<div class="divHeader">
<div class="float-left" >
<img alt="Missing Image" src="@String.Format("../../Images/{0}.png", Model.MajorAssembly)"/>
</div>
<div class="divHeaderOffset">
<div class="float-left divHalfSpace">
<label class="header">Major Assembly</label>
<label>@Model.MajorAssembly</label>
<label class="header">Sub Assembly</label>
<label>@Model.SubAssembly</label>
</div>
<div class="float-left divHalfSpace">
<label class="header">Component Assembly</label>
<label>@Model.ComponentAssembly</label>
<label class="header">Business Test</label>
<label>@Model.BusinessTest</label>
</div>
</div>
</div>
<br />
<fieldset class="fieldset">
<legend>Fix Details</legend>
<div class="divDetailMargin">
<div class="float-left divHalfSpace">
<label class="header">Rectification Code</label>
@Html.DropDownListFor(Function(x) x.FixedByFaultRectificationID, ViewBag.RectificationCodes))
</div>
<div class="float-left divHalfSpace">
<label class="header">Rectification Comment</label>
@Html.TextAreaFor(Function(x) x.FixedByComment, 4)
</div>
</div>
</fieldset>
<div class="divButtonHeader">
<div class="divThreeColumns">
</div>
<div class="divThreeColumns">
<input id="FixFault" type="submit" value=""/>
<input id="Back" type="button" value="" onclick="location.href='@Url.Action("Index", "Failures")'" />
</div>
<div class="divThreeColumns">
</div>
</div>
</section>
</section>
</fieldset>
End Using
コントローラーの POST は現在、空のモデルを返します。ただし、FixedByFaultRectificationID と FixedByComment は例外で、ビューで選択されたコンボ アイテムと TextAreaFor から正しく入力されます。
' POST: /Failures/FailureSignOff
<HttpPost> _
Function FailureSignOff(model As OpenBuildFaultsViewModel) As ActionResult
Stop
End Function
結果