0

I am creating a web application in VB.Net with MVC 4 and Entity Framework. I would like to submit a form that creates multiple entities at once. For example, if I have the simple entity...

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

In my view, I want to be able to take user input for the creation of multiple Employees at once and then post all of this data back to a controller action. Something like...

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

It would be great if I could say in my controller action...

<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

How should I post this chunk of data correctly and how do I access it in my controller? I looked at a related source in C# but could not replicate it.

Community
  • 1
  • 1
ElliotSchmelliot
  • 7,322
  • 4
  • 41
  • 64

1 Answers1

1

You would simply need to repeat the employee input fields for as many employees as you would like to have.

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>

Once submited, the model binding would take care of populating the list of Employees and setting the Name property.

I would recommend using something like jQuery to have a button to give the option to the user to dynamically add more fields to the screen.

Andy T
  • 10,223
  • 5
  • 53
  • 95
  • So you're suggesting an array? And the Name field corresponds to the properties. How do i make sure my controller recognizes these as Employee entities then? – ElliotSchmelliot Aug 06 '13 at 19:46
  • Model binding will be able to bind the POSTed items to the Collection. It will see that you are providing elements named 'Name' and see that the Employee type also has a Name property. It will then try to get the data into that property. Here is some info on model binding to a list: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx – Andy T Aug 06 '13 at 19:49
  • Excellent, I think I have that working. One last question: The Name field is being correctly passed in, but not the EmployeeID. I get a null error. This should auto-generate in my db though as it is the primary key. – ElliotSchmelliot Aug 06 '13 at 19:56
  • I usualy do something like this name="employees[0].Name" this way it knows that it's the employees collection in my model to populate. – the_lotus Aug 06 '13 at 19:56
  • Correct, you should not pass in the EmployeeId or do anything with it until the db has added the record and generated the Id. – Andy T Aug 06 '13 at 19:58
  • Aha! My own db error, forgot to actually set the ID field to auto-generate. Thanks! – ElliotSchmelliot Aug 06 '13 at 20:06