Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

MVC 3 (C#, ASP.NET 4) Child records page - help needed with example

Posted on 2012-04-02
6
1,841 Views
Last Modified: 2012-04-17
Hi,
I am having a really hard time finding a simple example of creating a view and related changes to model/controller(s) to display a child record <view>.

In other words - I am not trying to accomplish fancy master-detail display and CRUD operations on one view,

I just want to add an action on the master/parent records view, to open/jump to a <filtered?> child/detail view page.

This seems like an obvious thing, that you couldn't NOT have in an application  - but I can't seem to find a good simple example of it anywhere !

Sure it would be noce to have a list of child records on a master page as well - but right now I am just trying to wrap my head around this simple task !

And yes - I am just getting started with MVC + ASP.NET ;-)
0
Comment
Question by:bbaley
  • 3
  • 2
6 Comments
 

Author Comment

by:bbaley
ID: 37798574
I forgot to mention;

the model already has defined the EntityCollection/navigation/relationshipmanager/getrelatedcollction stuff related to the child/detail tables- which came from the Model Designer and db relationships - this was *not* CodeFirst....

for instance;

        #region Navigation Properties
   
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [XmlIgnoreAttribute()]
        [SoapIgnoreAttribute()]
        [DataMemberAttribute()]
        [EdmRelationshipNavigationPropertyAttribute("UGOPSModel", "FK_BargeDetail_Shift", "BargeDetail")]
        public EntityCollection<BargeDetail> BargeDetails
        {
            get
            {
                return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<BargeDetail>("UGOPSModel.FK_BargeDetail_Shift", "BargeDetail");
            }
            set
            {
                if ((value != null))
                {
                    ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<BargeDetail>("UGOPSModel.FK_BargeDetail_Shift", "BargeDetail", value);
                }
            }
        }
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37800561
Let me start by saying that I am trying to learn about MVC 3, since it is a little different than MVC 2.

Are you using Razor or ASP.NET views?

Parent Child List in MVC3 Razor
http://stackoverflow.com/questions/9341711/parent-child-list-in-mvc3-razor

@model IEnumerable<Product>
<ul>
    @foreach (var child in Model)
    {
        <li>
            @Html.DisplayFor(m => child.Name)
            @Html.Action("DisplayChildren", "ControllerName", 
                new { parentId = child.Id })
        </li>
    }
</ul>

Open in new window

0
 
LVL 22

Expert Comment

by:ambience
ID: 37800587
>> I just want to add an action on the master/parent records view, to open/jump to a <filtered?> child/detail view page.

This requires State, because the action depends upon the state (model) of the previous action. The way to do this is to either store in session the parent model and execute second action on say ChildController. The action would fetch the stored parent from session. Of course, using session is a decision that affects scalability of applications and for this simple learning task I suggest that you try it that way.

Another solution is to pass the parent's id to child action (store it as part of the url for action on main page). The child controller can re-fetch the parent object or perhaps just fetch collection of objects for given parent id. The downside is an additional DB hit.

Another interesting solution (which may not make sense in all cases) would be to store the json form of object on the client side will relationships eagerly loaded. Then for just child display, you can work entirely with Js and client end html.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:bbaley
ID: 37806354
:Abience;

Seriously?
It's this difficult ?

What application does NOT require the editing of child/detail records ?

How else could you possibly drill down into detail records ?

I just find it so hard to believe this requires any special handling and/or difficult to locate a "best practices" approach to doing so.
0
 
LVL 22

Accepted Solution

by:
ambience earned 500 total points
ID: 37806845
Not its not difficult - and every application that needs such a functionality also has to obtain the child object somehow

- Find it already stored in session  - stateful design
- Find it from datasource, need to know the child id - stateless design.

The main issue here is that it requires two HTTP request

1) fetch parent and display an edit link
2) open a page filled with children from previous request

The third approach (JSON) I mentioned tries to cache everything on the client side on first request and saves hitting the server again just to open a form.

>> How else could you possibly drill down into detail records ?

Normally from a master-detail list page you open directly the child edit page by passing it the child's id. Sophisticated solutions would put the retrieved children in a Cache like ASP.NET Cache.
0
 

Author Comment

by:bbaley
ID: 37808362
"The main issue here is that it requires two HTTP request

1) fetch parent and display an edit link
2) open a page filled with children from previous request"

----------------------------------------------

Thank you for the explanation -
this is what I am having a hard time finding an easy example of.


I found a project on MS asp.net MVC tutorial site - and it gives code-first examples of DISPLAYING a list of child records, so that's a good starting point, but it has no explanation - only code.

So I am trying to matchup the difference between the code-first examples and my non-code-first model and the different way of coding things...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question