我目前正在 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
结果