views:

26

answers:

1

I have an Employee model that has a Name and Company

I want to be able to build multiple Employees in one CREATE view. I figured I need to create an IList<Employee> and do something like:

<%= Html.TextBoxFor(m => m[0].Name) %>
<%= Html.TextBoxFor(m => m[0].Company) %>

<%= Html.TextBoxFor(m => m[1].Name) %>
<%= Html.TextBoxFor(m => m[1].Company) %>

If a user clicks on "Add another employee", I want the view to make another form for the new employee:

<%= Html.TextBoxFor(m => m[3].Name) %>
<%= Html.TextBoxFor(m => m[3].Company) %>

And continue to add form items (and increment the array index if they click on it again).

Keep in mind that I need to build the form and the list dynamically in the create view. I don't already have a populated list of Employees.

How should I do this? Code samples would be great, since I'm very new to ASP.NET

A: 

I answered a similar question yesterday
http://stackoverflow.com/questions/3199629/programmatically-add-controls-to-form/3199875#3199875

This example adds items to a List(Of T) on button click, but you can add items to a control list however you like.

Basically you create a class with multiple object properties to be stored within it.

Then you add item's to the collection.

Imports System.Collections.Generic


Partial Class Default2
    Inherits System.Web.UI.Page

    ''# we need to create an array of our control list class
    Public Shared _empList As List(Of EmployeeList)



    ''# button click event
    Protected Sub AddStuff

        ''# create a new employee
        Dim emp As Employee = New Employee
        With emp
            .Name = "Joe"
            .Company = "Acme Welding"
        End With

        ''# add the employee to our custom array
        _empList.Add(New ControlList(emp))

    End Sub



    ''# this is our custom Employee List
    ''# the idea behind this is for us to store 
    Public Class EmployeeList
        Private _employee As Employee
        Public Property Employee As Employee
            Get
                Return _employee
            End Get
            Set(ByVal value As Employee)
                _employee = value
            End Set
        End Property

        Public Sub New(ByVal employee As Employee)
            _employee = employee
        End Sub

    End Class


End Class

Note that you don't actually have to use the EmployeeList class to do this, you could simply create a list of the already existent "Employee" class that is built by L2S/EF, etc

Then you would pass _empList to the view and use a foreach loop to iterate through it.

EDIT:

Try something along these lines

public ActionResult ListEmployees()
{
    List<Employee> _empList = new List<Employee>();

    //'# create a new employee
    Employee emp = new Employee();
    {
        emp.Name = "Joe";
        emp.Company = "Acme Welding";
    }

    //'# add the employee to our custom array
    _empList.Add(new emp);

    return View(_empList);
}

Then in your view you do something like this

<%
foreach (var employee in Model)
{
    %>
    <p>Dude's Name: <%= Html.TextBoxFor(employee.Name) %></p>
    <p>Dude's Company: <%= Html.TextBoxFor(employee.Company) %></p>
    <%
}
%>
rockinthesixstring
Could you step me through in terms of C# if it's not too much trouble? Though you solution sounds like it would work.
Fred
see my edit above.
rockinthesixstring