VB   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了vb.net – 更新mvc4中的对象列表大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经解决了一个问题,我正在使用asp.net mvc4应用程序并简化它,@R_237_9447@在这里发布它.我的基本问题是我正在尝试将项目列表发送到我的视图并编辑任何项目的复选框,然后将列表项目发送回控制器并最终保存到数据库.当我将列表发送回控制器时,代码的编写方式就像一个空值一样,就像从未实例化过一样.

代码如下:

Public Class Person
    Property ID As Integer
    Property Name As String
    Property Active As Boolean
End Class

在控制器中,我调用了一个名为BuildPeople的类,它实际上只是一种构建要传递的列表的方法:

Public Class BuildPeople

    Public Function GetPersonList() As List(Of Person)
        Dim personList As New List(Of Person)
        personList.Add(GetPerson(1,"Chris",TruE))
        personList.Add(GetPerson(2,"Ken",TruE))
        personList.Add(GetPerson(3,"jen",TruE))
        Return personList
    End Function

    Private Function GetPerson(id As Integer,name As String,active As Boolean) As Person
        Dim p As New Person
        p.ID = id
        p.Name = name
        p.Active = active
        Return p
    End Function  
End Class

控制器只具有编辑功能:

Imports System.Web.Mvc

Public Class PeopleController
    Inherits Controller

    ' GET: /People
    Function Index() As ActionResult
        Return View()
    End Function

    ' GET: /People/Edit/5
    Function Edit() As ActionResult
        Dim bp As New BuildPeople
        Dim model As New List(Of Person)
        model = bp.GetPersonList
        ViewData.model = model
        Return View()
    End Function

    ' POST: /People/Edit/5
    <httpPost()>
    Function Edit(ByVal listPeople As List(Of Person)) As ActionResult
        Try
            ' TODO: Add update logic here
            If listPeople Is Nothing Then
                'Don't want to end up here
                Return View()
            Else
                'Want to end up here
                Dim i As Integer = listPeople.Count
                Return View()
            End If
        Catch
            Return View()
        End Try
    End Function
End Class

然后视图如下:

@modelType List(Of Person)
@Code
    ViewData("title") = "Edit"
    Layout = "~/Views/Shared/_Layout.vbhtml"
End Code

<h2>Edit</h2>

@Using (Html.beginForm())
    @Html.AntiForgeryToken()

    @<div class="form-horizontal">
        <h4>Person</h4>
        <hr />
        @For Each item In Model
        Dim currentitem = item
            @Html.HiddenFor(Function(model) currentitem.ID)
            @Html.EditorFor(Function(model) currentitem.Name)
            @Html.EditorFor(Function(model) currentitem.ActivE)
        Next

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

<div>
    @Html.ActionLink("BACk to List","Index")
</div>
因此,您必须了解HTML表单的工作方式以及MVC模型绑定器的工作原理.

如果选中复选框,则仅在后期数据中发回一个值.

接下来,只要字段命名遵循正确的命名约定,MVC中的模型绑定器将重新构建集合/列表对象.

因此,对于模型中的每个项目,您的循环需要生成具有正确名称的项目.

让我们稍微改变你的模型.

Public Class PeopleModel
  Public Property People As List(Of Person)
  Public Property SELEctedPeople As List(Of Int64)  ' Assuming Person.ID is Int64
End Class

然后你改变你的视图循环,如下所示:

Dim itemIndex As Int32 = 0
For Each person As Person In Model.People
  @<input type='checkbox' name='SELEctedPeople[@itemIndex]' id='person_@person.ID' value='@person.ID' />
  @<input type='hidden' name='SELEctedPeople[@itemIndex]' value='-1' />
  itemIndex += 1
Next

我们将隐藏元素放在那里,因为它将为未检查的项提供字段值.否则,第一个未检查的项目将破坏索引,模型绑定器将停止.

现在在你的控制器的post处理程序中:

<httpPost>
Public Function ActionName(model As PeopleModel) As ActionResult

  For Each id As Int64 In model.SELEctedPeople
    If 0 < id Then
      ' This is the id of a SELEcted person - do something with it.
    End If
  Next

End Function

大佬总结

以上是大佬教程为你收集整理的vb.net – 更新mvc4中的对象列表全部内容,希望文章能够帮你解决vb.net – 更新mvc4中的对象列表所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。