0

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

结果

在此处输入图像描述

4

2 回答 2

0

尝试从属性中 删除ReadOnly关键字。Failure

Public ReadOnly Property Failure As OpenBuildFaultsViewModel

我认为模型绑定器将无法设置属性,因为只读属性只能在类的声明或构造函数中设置。

于 2012-08-21T12:03:29.753 回答
0

好的,经过对 MVC 和模型绑定的进一步调查,我已经解决了这个问题。在重构代码以使用 ViewBag 将一些数据传递到视图中并通过 ViewBag 绑定视图数据之后,我可以将一个更简单的模型传递给视图。

HTTPPost 操作中未返回模型的原因是,并非模型中的所有项目都绑定在视图中,如本文所述

工作视图

@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>

                        @Html.Hidden("FailureID", Model.FailureID)
                        @Html.Hidden("ModelName", Model.ModelName)
                        @Html.Hidden("ZoneName", Model.ZoneName)
                        @Html.Hidden("Fault", Model.Fault)
                        @Html.Hidden("FaultCode", Model.FaultCode)
                        @Html.Hidden("FaultCodeDetail", Model.FaultCodeDetail)
                        @Html.Hidden("FaultArea", Model.FaultArea)
                        @Html.Hidden("AuditScore", Model.AuditScore)
                        @Html.Hidden("Comment", Model.Comment)
                        @Html.Hidden("ShortagePart", Model.ShortagePart)
                        @Html.Hidden("CreatedBy", Model.CreatedBy)
                        @Html.Hidden("FixedByID",model.FixedByID)

                        <div class="divHeaderOffset">
                            <div class="float-left divHalfSpace">
                                <label class="header">Major Assembly</label>
                                @Html.TextBoxFor(Function(x) x.MajorAssembly, New With {.ReadOnly = True})
                                <label class="header">Sub Assembly</label>
                                @Html.TextBoxFor(Function(x) x.SubAssembly, New With {.ReadOnly = True})
                            </div>
                            <div class="float-left divHalfSpace">
                                <label class="header">Component Assembly</label>
                                @Html.TextBoxFor(Function(x) x.ComponentAssembly, New With {.ReadOnly = True})
                                <label class="header">Business Test</label>
                                @Html.TextBoxFor(Function(x) x.BusinessTest, New With {.ReadOnly = True})
                            </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

工作控制器

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(DirectCast(model.FirstOrDefault, OpenBuildFaultsViewModel))

        End Function

        ' POST: /Failures/FailureSignOff
        <HttpPost> _
        Function FailureSignOff(model As OpenBuildFaultsViewModel) As ActionResult

            Stop

        End Function

    End Class

End Namespace

进行这些更改后,视图将正确填充回 HTTPPost 上的控制器。

返回模型

于 2012-08-22T07:49:38.803 回答