简体   繁体   English

使用MVC2的EditTemplates和相关对象

[英]EditTemplates and related objects with MVC2

I am learning MVC using the v2 release with Entity Framework v4. 我正在使用v2和Entity Framework v4一起学习MVC。 Let's say I have 3 objects Game, Points and Players. 假设我有3个对象:游戏,积分和玩家。 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). 它们以以下方式关联:游戏具有点,并且点可以具有与其相关联的玩家(1个游戏到多个点,并且一个点对象可以具有一个玩家)。

I am attempting to use the EditTemplates feature in MVC2 to render my views. 我正在尝试使用MVC2中的EditTemplates功能来呈现我的视图。 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. 目前,我正在使用“ <%= Html.EditorForModel() %> ”(这似乎很慢)来呈现“编辑视图”,然后有一个特定的Game和Point EditTemplates。

The data renders correctly and is editable for both the Game and Point information. 数据可以正确呈现,并且对于Game和Point信息都是可编辑的。 When I go to perform the update and submit the form I receive the “Game” object in my Update ActionResult. 当我去执行更新并提交表单时,我在更新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. 如果我在调试中查看Request.Form变量,则可以看到Points字段正在传递给服务器,但没有将其放回Game对象中。

In my Game EditTemplate I am using the following to render the Points objects: 在我的游戏EditTemplate中,我使用以下方法渲染Points对象:

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

My Points EditTemplate looks like: 我的积分EditTemplate看起来像:

<%@ 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: 我呈现的HTML如下所示:

<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? 如何获取深层的属性,以发布回Controller Update ActionResult接受的Game对象中,以便同时更新它们?

UPDATE: This definitely seems to be an issue with the way the EditTemplate is rendering the Points collection. 更新:这显然与EditTemplate渲染Points集合的方式有关。 If I Manually add the following to the view it does appear correctly in the Game object: 如果我手动将以下内容添加到视图,则它确实会正确显示在Game对象中:

<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: 知道为什么将其渲染为“ Points.Points [index]而不是Game.Points [index]吗?我试图弄乱EditFor中的参数:

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

but then the inputs render as Game.Points.Game.Points[index] 但随后输入呈现为Game.Points.Game.Points [index]

This is apparently a bug in the early releases of MVC. 这显然是MVC早期版本中的错误。 According to the comment I received from Microsoft on the MVC forum it looks to be fixed in the RTM version. 根据我在MVC论坛上从Microsoft收到的评论,它似乎已在RTM版本中修复。

I've revert back to using the for loop to generate the HTML in the mean time. 同时,我又回到了使用for循环来生成HTML的过程。

Here is the MVC forum response for reference: http://forums.asp.net/t/1515461.aspx 这是MVC论坛的响应,以供参考: http : //forums.asp.net/t/1515461.aspx

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. 它们必须被命名为Game.Points [0] .PntId,或者您可以将Points添加为您的操作的参数,然后将它们组合起来。 Especcially if you are persisting them in DB, you gona have to attach them. 特别是如果要将它们持久存储在数据库中,则必须附加它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM