2

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 View

<%@ 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;
    }
}
Picflight
  • 3,832
  • 13
  • 61
  • 90

1 Answers1

0

Hopefully you've found an answer to this elsewhere, although based on how difficult it is to find information about this online, it's probably unlikely ...

An MSDN blog linked over to ASP.NET MVC, Entity Framework, Modifying One-to-Many and Many-to-Many Relationships (there's a link to the previous in the series in the first paragraph).

But Editing a variable length list, ASP.NET MVC 2-style seems a little better (and includes sample code).

James Skemp
  • 8,018
  • 9
  • 64
  • 107
  • Steve's Knockout post is an even better way to edit one-to-many relationships. http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/ – Ryan Mar 02 '11 at 01:26
  • @Ryan Thanks for the additional link. I thought that example looked familiar, and now I know why. Unfortunate that he didn't link the older to the newer, but ... good find! – James Skemp Mar 02 '11 at 18:51
  • 1
    @KasperSkov, I think it might be a GitHub issue. The frame source is http://demo.stevensanderson.com/ko_gift_editor/ which is 404ing for me. But I also got the message in Chrome (in a message box). http://knockoutjs.com/examples/gridEditor.html might be a semi-match. (No email link, and I stopped using Twitter, so I've given up trying to contact Steve about the issue.) – James Skemp Nov 29 '12 at 01:21
  • Please see [this](http://stackoverflow.com/questions/29324837/add-related-entities-with-asp-net-mvc-and-razor) question. – Shimmy Weitzhandler Mar 29 '15 at 02:08