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

时间:2013-08-06 19:35:29

标签: asp.net-mvc vb.net entity-framework asp.net-mvc-4

我正在使用MVC 4和Entity Framework在VB.Net中创建一个Web应用程序。我想提交一个一次创建多个实体的表单。例如,如果我有简单的实体......

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

在我看来,我希望能够同时获取用户输入以创建多个Employees,然后将所有这些数据发布回控制器操作。有点像...

@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#中查看了related source但无法复制它。

1 个答案:

答案 0 :(得分:1)

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

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这样的东西,让按钮为用户提供动态添加更多字段到屏幕的选项。