gpt4 book ai didi

vb.net - 更新 mvc4 中的对象列表

转载 作者:行者123 更新时间:2023-12-04 04:35:28 26 4
gpt4 key购买 nike

我已经解决了我在使用 asp.net mvc4 应用程序时遇到的问题并对其进行了简化,以便我可以将其发布在这里。我的基本问题是我试图将项目列表发送到我的 View 并编辑任何项目的复选框,然后将列表项目发送回 Controller 并最终保存到数据库。当我将列表发送回 Controller 时,现在编写代码的方式就像它从未被实例化一样,它作为一个空值出现。

代码如下:

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

在 Controller 中,我调用了一个名为 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

Controller 只有编辑能力:
    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

然后 View 如下:
@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 模型绑定(bind)器的工作原理有所了解。

复选框仅在帖子数据中发回一个值 如果他们被检查 .

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

所以你的循环For Each item In Model需要生产具有正确名称的项目。

让我们稍微改变一下你的模型。

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

然后你改变你的 View 循环,如下所示:
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

我们将隐藏元素放在那里,因为它会为未选中的项目提供一个字段值。否则,第一个未选中的项目会破坏索引并且模型绑定(bind)器将停止。

现在在您的 Controller 的后处理程序中:
<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 中的对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19823604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com