模型数据未在事件后发生 [英] model data not coming through on post event

查看:40
本文介绍了模型数据未在事件后发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我无法在帖子上获取模型数据。下面是控制器和视图的代码。

Hello, I am having trouble getting model data on a post. Below is the code for the controller, and view.

Function LineItems(ByVal id As Integer?) As ActionResult

            If IsNothing(id) = True Then
                Return HttpNotFound()
            End If

            ' this is the maximum number of line items on the
            ' purchase order file
            Dim numberOfLineItems As Integer = 24

            Dim _model As New POViewModel.lineItems()
            _model.tbl_po = db.tbl_po.Find(id)

            ' get the user id
            Dim _userQ = From _person In db.tbl_people
                         Where _person.username = User.Identity.Name
                         Select _person
            Dim _userId As Integer = _userQ.ToList(0).id

            Dim poQuery = From _po As tbl_po In db.tbl_po
                          Order By _po.number
                          Where _po.issuedTo = _userId
                          Select _po

            _model.poNumbers = New SelectList(poQuery.ToList(), "number", "number")

            If IsNothing(id) = False Then

                Dim lineItemQuery = From _lineItem As tbl_po_lineItem In db.tbl_po_lineItem
                                    Where _lineItem.poNumber = _model.tbl_po.number
                                    Order By _lineItem.id
                                    Select _lineItem

                Dim _lineItemList As List(Of tbl_po_lineItem) = lineItemQuery.ToList()
                If lineItemQuery.ToList().Count < numberOfLineItems Then

                    Dim _index As Integer = 0
                    For _index = 0 To numberOfLineItems - 1
                        If _index >= _lineItemList.Count = True Then

                            Dim newLineItem As New tbl_po_lineItem()
                            newLineItem.poNumber = _model.tbl_po.number
                            _lineItemList.Add(newLineItem)

                        End If
                    Next _index

                End If
                _model.lineItems = _lineItemList
            End If

            Return View(_model)
        End Function

        <HttpPost()>
        <ValidateAntiForgeryToken()>
        Function LineItems(ByVal _model As POViewModel.lineItems) As ActionResult

            If IsNothing(_model.lineItems) = False Then

                For Each _item As tbl_po_lineItem In _model.lineItems

                    If IsNothing(_item.id) = False And IsDBNull(_item.id) = False Then

                        ' need to update this line item then
                        Dim theItem As tbl_po_lineItem = db.tbl_po_lineItem.Find(_item.id)
                        theItem.qty = _item.qty
                        theItem.lineItem = _item.lineItem
                        theItem.amount = _item.amount

                    Else
                        db.tbl_po_lineItem.Add(_item)
                    End If

                Next _item

                db.SaveChanges()

                Return RedirectToAction("Index")

            End If
            Return View(_model)
        End Function


@ModelType POViewModel.lineItems
@Code
    ViewData("Title") = "Line Items"
    Layout = "~/Views/Shared/_LayoutPO.vbhtml"
End Code

<h2>line items</h2>
<p>edit the indicated line items for the selected purchase order</p>

@Using Html.BeginForm()

    @Html.AntiForgeryToken()

    @<div class="form-horizontal">
        @Html.ValidationSummary(True, "data provided is invalid", New With {.class = "text-danger"})
        
         <div class="form-group">
             @Html.LabelFor(Function(model) model.tbl_po.number, "po number", htmlAttributes:=New With {.class = "control-label col-md-2"})
             <div class="col-md-10">
                 @Html.EditorFor(Function(model) model.tbl_po.number, New With {.htmlAttributes = New With {.class = "form-control", .disabled = "disabled"}})
                 @Html.ValidationMessageFor(Function(model) model.tbl_po.number, "", New With {.class = "text-danger"})
             </div>
         </div>

        <table class="table">
            <tr>
                <td width="10%">
                    @Html.LabelFor(Function(model) model.tbl_po_lineItem.qty, "qty", htmlAttributes:=New With {.class = "control-label col-md-2"})
                </td>
                <td width="80%">
                    @Html.LabelFor(Function(model) model.tbl_po_lineItem.lineItem, "line item", htmlAttributes:=New With {.class = "control-label col-md-2"})
                </td>
                <td width="10%">
                    @Html.LabelFor(Function(model) model.tbl_po_lineItem.amount, "amount", htmlAttributes:=New With {.class = "control-label col-md-2"})
                </td>
            </tr>
            @For Each _item As tbl_po_lineItem In Model.lineItems
                @<tr>
                    <td>
                        @Html.EditorFor(Function(Model) _item.qty, New With {.htmlAttributes = New With {.class = "form-control"}})
                    </td>
                    <td>
                        @Html.EditorFor(Function(Model) _item.lineItem, New With {.htmlAttributes = New With {.class = "form-control"}})
                    </td>
                    <td>
                        @Html.EditorFor(Function(Model) _item.amount, New With {.htmlattributes = New With {.class = "form-control"}})
                    </td>
                    <td>
                        @Html.ValidationMessageFor(Function(Model) _item.qty, "", New With {.class = "text-danger"})
                        @Html.ValidationMessageFor(Function(Model) _item.lineItem, "", New With {.class = "text-danger"})
                        @Html.ValidationMessageFor(Function(Model) _item.amount, "", New With {.class = "text-danger"})
                    </td>
                </tr>
            Next _item
        </table>

         <div class="form-group">
             <div class="col-md-offset-2 col-md-10">
                 <input type="submit" value="saveLineItems" class="btn btn-default" />
             </div>
         </div>
    </div>

End Using

任何帮助表示赞赏。控制器发布事件中的对象全部为"无"。

Any help is appreciated. The object in the controller post event is all "nothing".

neech

推荐答案

以下是模型类:

Here is the model class:

    Public Class lineItems

        Public Property tbl_po As tbl_po
        Public Property tbl_po_lineItem As tbl_po_lineItem
        Public Property lineItems As List(Of tbl_po_lineItem)
        Public Property poNumbers As IEnumerable(Of SelectListItem)

    End Class


这篇关于模型数据未在事件后发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆