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

OK、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 に答える
0

プロパティ ReadOnlyからキーワードを削除してみてください。Failure

Public ReadOnly Property Failure As OpenBuildFaultsViewModel

読み取り専用プロパティはクラスの宣言またはコンストラクタでのみ設定できるため、モデル バインダーはプロパティを設定できないと思います。

于 2012-08-21T12:03:29.753 に答える