views:

117

answers:

4

I have a Company model and an Employee model and I want to create a Company and then create multiple employees for the company in one view.

How should I do this in the view?

And what should I do in the Create POST method to support this multi-entry view?

A: 

I really have no idea what you are talking about but here's a wild guess: One entry Foo, multiple enries, IEnumerable<Foo>. Sounds familiar?

Darin Dimitrov
A: 

If I understand the question, I think this article is relevant: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

My suggestion is to try to take advantage of the default model binder's behavior by naming your fields appropriately. Refer to the article for specific naming conventions for various situations.

EDIT: added the example view logic below. Note, calling Html.Hidden and Html.Input with the third parameter as an anonymous type with the id property specified is not really required; that's my preference for ensuring that those functions don't produce HTML elements with invalid id attributes (containing square brackets).

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
        <title>Example</title>
</head>
<body>
        <div><%
// Get your list of data objects from the model somehow.  This will be specific to your model
IEnumerable<MyData> items = Model as IEnumerable<MyData>;

// if there are items
if (items != null)
{
    // then iterate over them to create fields inside of a form
    using (Html.BeginForm())
    {
        int itemIndex = 0;
        foreach (var item in items)
        { %>
            <div>
                <%= Html.Hidden("MyData[" + itemIndex + "].MyDataID", item.MyDataID, new { id = "MyData_" + itemIndex + "_MyDataID" })%>
                <%= Html.TextBox("MyData[" + itemIndex + "].FirstName", item.FirstName, new { id = "MyData_" + itemIndex + "_FirstName" })%>
                <%= Html.TextBox("MyData[" + itemIndex + "].LastName", item.LastName, new { id = "MyData_" + itemIndex + "_LastName" })%>
            </div><%
                             itemIndex++;
        }
    }   
} %>
        </div>
</body>
</html>

You can then have an action method in your controller to handle the posted data:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Example(IList<MyData> MyData)
    {
        // the MyData parameter will automatically be populated by the default ModelBinder using the values stored in the form collection (the posted data).
    }
Dr. Wily's Apprentice
This seems to be the right direction. But I wonder how I can generate the fields on the fly in the view.
Fred
A: 

If the default model binder (the object that converts the form values to the object you're using as a parameter in the action method) is not sufficient for your purposes, you might consider using a custom model binder

Jaco Pretorius