views:

412

answers:

2

I am learning MVC using the v2 release with Entity Framework v4. Let’s say I have 3 objects Game, Points and Players. They are related in the following manner: Game has points and the Points can have a player associated with them ( 1 Game to many Points and a Point object can have one Player).

I am attempting to use the EditTemplates feature in MVC2 to render my views. In my Game Edit view I want to have the basic Game object information editable, and also the related Points objects. Currently I am utilizing “<%= Html.EditorForModel() %>“ (Which seems pretty slow) to render the Edit View and then I have a specific Game and Point EditTemplates.

The data renders correctly and is editable for both the Game and Point information. When I go to perform the update and submit the form I receive the “Game” object in my Update ActionResult. The basic properties are populated for the Game object but any deep properties such as Points are not; they appear as null. If I look at the Request.Form variables in debug I can see the Points fields are being passed to the server but do not place themselves back into the Game object.

In my Game EditTemplate I am using the following to render the Points objects:

<%= Html.EditorFor(c => c.Points) %>

My Points EditTemplate looks like:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Domain.Model.Entities.Point>" %>

<%= Html.EditorFor(c => c.PntId)%>
<tr><td><%= Html.DisplayFor(c => c.User.Username)%></td><td><%= Html.EditorFor(c => c.UserPnt)%></td></tr>

My HTML that is rendered looks like the following:

<input id="Points_Points_0__PntId" name="Points.Points[0].PntId" type="hidden" value="226" />
<tr><td>Jay</td><td><input class="text-box single-line" id="Points_Points_0__UserPnts" name="Points.Points[0].UserPnts" type="text" value="20" /></td></tr>

<input id="Points_Points_1__PntId" name="Points.Points[1].PntId" type="hidden" value="227" />
<tr><td>Joe</td><td><input class="text-box single-line" id="Points_Points_1__UserPnts" name="Points.Points[1].UserPnts" type="text" value="20" /></td></tr>

How can do I get the deep Properties to post back in the Game object that is accepted by the Controller Update ActionResult so that I can update them at the same time?

UPDATE: This definitely seems to be an issue with the way the EditTemplate is rendering the Points collection. If I Manually add the following to the view it does appear correctly in the Game object:

<input class="text-box single-line" id="Game_Points_0__UserPnts" name="Game.Points[0].UserPnts" type="text" value="20" />
        <input class="text-box single-line" id="Game_Points_1__UserPnts" name="Game.Points[1].UserPnts" type="text" value="20" />

Any idea why this is rendering as "Points.Points[index] instead of Game.Points[index]? I tried messing with the parameters in the EditFor:

<%= Html.EditorFor(c => c.Points,null,"Game.Points") %>

but then the inputs render as Game.Points.Game.Points[index]

A: 

They have to be named Game.Points[0].PntId or you can add Points as an argument to your action and combine them in action. Especcially if you are persisting them in DB, you gona have to attach them.

Alexander Taran
I wondered if the issue is because it is rendering it as "Points.Points[index]". this didn't look correct. I do not see a way to control the name that the "Points" input are rendered. This is done automatically by MVC when I tell it to use "Html.EditorFor(c => c.Points)"I tried to add "IList<Point> Points" as a parameter to my Update ActionResult but this returned null as well.
Jay
+1  A: 

This is apparently a bug in the early releases of MVC. According to the comment I received from Microsoft on the MVC forum it looks to be fixed in the RTM version.

I've revert back to using the for loop to generate the HTML in the mean time.

Here is the MVC forum response for reference: http://forums.asp.net/t/1515461.aspx

Jay