views:

403

answers:

1

I am just playing around with MVC for the first time and am having a problem.

Let's say I have 2 tables - Person and Location. Location has a foreign key relationship to Person - so 1 person can have multiple locations belonging to them.

In my view I have form inputs for the Person fields - this works fine.

However, I also want to have a grid to hold Locations, (with the standard Add/Delete buttons), also master-detail form inputs that are tied to the current record in the Locations grid.

My problem is how do I implement the child relation grid and form inputs in the view?

Thanks in advance.

+1  A: 

You can have another form for the Locations with its own Add/Save/Submit button. So when submssion happens from the Person form, it will update the Person record, but if the submission happens from the Location form it will do the appropriate action.

Like this:

// person data
<table>
    <tr><td>Name:</td><td><%=Model.PersonName%></td></tr>
    <tr><td>Title:</td><td><%=Model.PersonTitle%></td></tr>
    <tr><td>Other Info:</td><td><%=Model.PersonOtherInfo%></td></tr>
</table>
// person's locations grid
<table>
    <tr><th></th><th></th></tr>
    <% foreach (Location loc in Model.PersonLocations) { %>
    <tr><td>Delete</td><td><%=loc.LocationName%></td></tr>
    <% } %>
</table>
// new location form
<% using (Html.BeginForm<MyController>(p => p.New())) { %>
<table>
    <tr>
        <td>Location Name</td>
        <td><%=Html.TextBox("LocationName")%></td>
    </tr>
    <tr>
        <td colspan="2">
            <%=Html.SubmitButton("Save", "Save")%>&nbsp;
            <%=Html.Button("Cancel", "Cancel", HtmlButtonType.Button,"javascript:cancelEdit();") %>
        </td>
    </tr>
</table>
<%=Html.AntiForgeryToken() %>
<%}%>
Johannes Setiabudi