VB MVC 4从表单提交创建多个实体



我正在VB.Net中使用MVC 4和实体框架创建一个web应用程序。我想提交一个同时创建多个实体的表单。例如,如果我有一个简单的实体。。。

Public Class Employee
    Public Property EmployeeID As Integer
    Public Property Name As String
End Class

在我看来,我希望能够接受用户输入,一次创建多个员工,然后将所有这些数据发布回控制器操作。类似。。。

@Using Html.BeginForm("AddEmployees")
    here is input for first employee
    here is input for second employee
    etc...
End Using

如果我能在我的控制器动作中说。。。

<HttpPost()>
Function AddEmployees(SomeCollection As Collection(Of Employee)) As ActionResult
    For Each item in SomeCollection
        do stuff with data...
    Next
    Return RedirectToAction("Index")
End Function

我应该如何正确发布这段数据,以及如何在控制器中访问它?我在C#中查看了一个相关的源代码,但无法复制它。

您只需要为想要的员工数量重复员工输入字段。

Name:   <input type="text" name="[0].Name" value="" /><br>
Name:   <input type="text" name="[1].Name" value="" /><br>
Name:   <input type="text" name="[2].Name" value="" /><br>
Name:   <input type="text" name="[3].Name" value="" /><br>

提交后,模型绑定将负责填充Employees列表并设置Name属性。

我建议使用类似jQuery的按钮,为用户提供在屏幕上动态添加更多字段的选项。

最新更新