问题描述
我修改了的应用程序中加入以下code将DinnerForm.ascx允许的子记录编辑
I have modified the Nerd Dinner application to allow editing of child records by adding the following code to the DinnerForm.ascx
<p>
<%int i = 0;
foreach (NerdDinner.Models.RSVP rsvp in this.Model.Dinner.RSVPs)
{ %>
<%= Html.Hidden("Dinner.RSVPs[" + i + "].RsvpID", rsvp.RsvpID)%>
<%= Html.Hidden("Dinner.RSVPs[" + i + "].DinnerID", rsvp.DinnerID)%>
<%= Html.TextBox("Dinner.RSVPs[" + i + "].AttendeeName", rsvp.AttendeeName)%>
<% i += 1;
} %>
</p>
它呈现这样的:
<p>
<input id="Dinner_RSVPs[0]_RsvpID" name="Dinner.RSVPs[0].RsvpID" type="hidden" value="36" />
<input id="Dinner_RSVPs[0]_DinnerID" name="Dinner.RSVPs[0].DinnerID" type="hidden" value="63" />
<input id="Dinner_RSVPs[0]_AttendeeName" name="Dinner.RSVPs[0].AttendeeName" type="text" value="kp" />
<input id="Dinner_RSVPs[1]_RsvpID" name="Dinner.RSVPs[1].RsvpID" type="hidden" value="37" />
<input id="Dinner_RSVPs[1]_DinnerID" name="Dinner.RSVPs[1].DinnerID" type="hidden" value="63" />
<input id="Dinner_RSVPs[1]_AttendeeName" name="Dinner.RSVPs[1].AttendeeName" type="text" value="jim" />
</p>
我没有修改DinnersControler的帖子编辑操作方法。家长晚宴得到更新像往常一样,但它出现在的UpdateModel(晚餐);是不是看到/更新孩子RSVP记录。
I have not modified the DinnersControler's Post Edit Action method. The Parent dinner is getting updated as usual, but it appears the UpdateModel(dinner); is not seeing/updating the child RSVP records.
我已经试过渲染子记录,使模型粘合剂将看到集合,没有运气有一些变化。
I have tried a few variations on rendering the child records so that the Model binders will see the collection, with no luck.
是调用的UpdateModel(家长)更新一次性父/子记录;可能与现款车型粘合剂?
Is updating parent/child records in one shot by calling UpdateModel(Parent); possible with the current model binders?
推荐答案
我一直没能做到这一点我自己。
I haven't been able to do this myself.
我知道,你可以更新一个子元素,即Dinner.RSV自动。我从来没有见过更新孩子枚举,这需要结合知道哪个属性是ID,并寻找它(即Dinner.RSVP.Where(R => r.RSVP_ID == input_id),然后是能力更新)。我不知道有足够的了解自定义绑定到做这样的事情。
I know that you can update a single child element, ie, Dinner.RSV automatically. I've never seen the ability to update a child enumerable, which would require the binding to know which property is the ID and look for it (ie, Dinner.RSVP.Where(r => r.RSVP_ID == input_id) and then update that). I don't know enough about custom binding to do something like that.
不过,我所做的是做一个循环,并指定RSVP和INT为preFIX:
However, what I have done is to do a loop and specify the rsvp and the int as a prefix:
所以,你做的:
UpdateModel("Dinner", Dinner);
更新父,然后:
int i = 0;
foreach (var r in Dinner.RSVPs) {
UpdateModel(r, "Dinner.RSVPs[" + i++ + "]");
}
不太干净,但它很适合我。这可能需要多一点倾力打造的验证,但(要验证所有在同一时间,并确保你不出错跳回视图上的第一个RSVP)。
Not quite as clean, but it works well for me. It might take a bit more effort to build in validation, though (you want to validate all at the same time, and make sure you don't jump back to the view on the first RSVP with an error).
修改:修正了code,以反映OP的解决方案,包括在我的参数顺序的错误。有了这样说,我是更舒适的使用比正在运行的整数RSVP.ID财产。只要你知道Dinner.RSVPs将在POST为得到相同的(我在我的code自信这一点),然后使用RSVP.ID会工作。应该回函是不同的,那么只有那些在$均P $ psent将得到更新。然而,使用连续的INT有可能导致错误的对象被更新。
EDIT: Fixed the code to reflect the OP's solution, including a bug in my parameter order. With that being said, I'm more comfortable using the RSVP.ID property than a running integer. As long as you know that Dinner.RSVPs will be the same on the POST as the GET (I'm confident of this in my code), then using the RSVP.ID will work. Should RSVPs be different, then only those present on both will get updated. However, using the sequential int could potentially cause the wrong object to be updated.
希望帮助,
詹姆斯
Hope that helps,James
这篇关于更新父/子记录与ASP.Net MVC模型粘合剂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!