How to Add, Edit and Display one to many relationship entities in ASP.Net MVC 2?

Posted by Picflight on Stack Overflow See other posts from Stack Overflow or by Picflight
Published on 2010-03-12T23:50:33Z Indexed on 2010/03/12 23:57 UTC
Read the original article Hit count: 664

I am looking for best practices conforming to the MVC design pattern.

My Entities have the following relationship.
tblPortal PortalId PrortalName
tblPortalAlias AliasId PortalId HttpAlias

Each Portal can have many PortalAlias.

I want to Add a New Portal and then Add the associated PortalAlias.

I am confused on how I should structure the Views and how I should present the Views to the user. I am looking for some sample code on how to accomplish this.

My thoughts are first present the Portal View, let the user add the Portal. Then click the Edit link on the Portal List View and on the Portal Edit View let them Add the PortalAlias.

If so, what should the Edit View look like?

So far I have:

Edit

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<MyProject.Mvc.Models.PortalFormViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit</h2>
<% Html.RenderPartial("PortalForm", Model); %>
<div>
    <%= Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>

PortalForm

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyProject.Mvc.Models.PortalFormViewModel>" %>
<%= Html.ValidationSummary("Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%>
    <%= Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Fields</legend>

       <div class="editor-label">
            <%= Html.LabelFor(model => model.Portal.PortalId) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Portal.PortalId) %>
            <%= Html.ValidationMessageFor(model => model.Portal.PortalId) %>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Portal.PortalName) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Portal.PortalName) %>
            <%= Html.ValidationMessageFor(model => model.Portal.PortalName) %>
        </div>            
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

<% } %>
Alias<br /><%-- This display is for debug --%>
<% foreach (var item in Model.PortalAlias) { %>

<%= item.HTTPAlias %><br />

<% } %>

PortalFormViewModel

public class PortalFormViewModel
{
    public Portal Portal { get; private set; }
    public IEnumerable<PortalAlias> PortalAlias { get; private set; }

    public PortalFormViewModel()
    {
        Portal = new Portal();
    }

    public PortalFormViewModel(Portal portal)
    {
        Portal = portal;
        PortalAlias = portal.PortalAlias;
    }
}

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about entity-framework