ASP.NET MVC UpdateModel与集合的层次结构绑定(ASP.NET MVC UpdateModel binding with a Hierarchy of Collections)

编程入门 行业动态 更新时间:2024-10-19 04:25:05
ASP.NET MVC UpdateModel与集合的层次结构绑定(ASP.NET MVC UpdateModel binding with a Hierarchy of Collections)

我有以下模型设计。

模板包含类别列表。 类别包含项目列表。

因此有3层分层结构。 因为UI设计要求在1页上创建所有3个级别,所以我必须编写一些自己的代码才能使其工作。 它看起来如下:

我可以将它提交回创建操作方法,FormCollection如下所示。

我遇到的问题是UpdateModel适用于模板和类别级别,但未为类别填充项目。 该Html看起来如下。 正如你所看到的,我使用了id的[guid_value] .Items [guid_value]语法。 它不应该根据它自动判断绑定吗?

有什么我失踪或必须做的,为了让绑定工作?

I have the following Model design.

Template contains a list of Categories. Categories contains a List of Items.

Thus there is 3 layer hierarchy. Because the UI design requires that all 3 levels be created on 1 page, I have had to write some of my own code to get this working. It looks as follows:

I can get it to submit back to the Create Action Method and the FormCollection looks as follows.

The problem I am having is that the UpdateModel works for the Template and Category level but the Items are not populated for the Categories. The Html looks as follows. As you can see, I have used the Category[guid_value].Items[guid_value] syntax for id. Shouldn't it figure out the binding automagically based on that?

Is there something I am missing or have to do in order to get the binding to work?

最满意答案

如果你想让你的html正确地绑定到你的模型上,你必须注意你渲染的html。 我可以看到你的html的问题是

<input type='hidden' value = 'Items.Index' value= 'some_guid' autocomplete = 'off'/>

与此HTML模型联编程序将尝试绑定项目作为模板对象的子对象,它不会找到任何名为项目的属性,这些值将不会绑定到模型。 你必须改变你的隐藏领域

<input type='hidden' name = 'Categories[category's guid].Items.Index' value = 'some guid' autocomplete = 'off'/>

你必须在你的action方法中调用updatemodel

UpdateModel(yourobject);

我没有测试过这段代码,但我更确定的是,以这种方式更改你的html会让你排序。 @Mlchael Grassman你不必使用基于整数的id来绑定集合了。 PLZ阅读史蒂夫的帖子以获取更多信息。 实际上,你不需要GUID,而是一个随机数,它是页面上下文中唯一的数字,只要你使用名称索引和值设置为上述随机数的额外隐藏字段即可。 我已经使用(和修改)Steve编写的BeginCollectionItem方法在asp.net mvc中发表了有关Master详细信息表单的博客。 在这个博客系列的第一部分中,我简单地展示了如何在没有Ajax调用的情况下在客户端动态添加您的详细记录的字段。 在第二部分中,我将详细记录的编辑器模板带到了jquery模板引擎,并将其呈现在客户端,就像通过ajax调用呈现时一样。

if you want your html to bind correctly to your model you have to pay some attention to html you render. The problem that i can see with your html is

<input type='hidden' value = 'Items.Index' value= 'some_guid' autocomplete = 'off'/>

with this html model binder will try to bind Items as children of Template object where it will not find any property named Items and these values will not be bound to the model. you have to change your hidden field to

<input type='hidden' name = 'Categories[category's guid].Items.Index' value = 'some guid' autocomplete = 'off'/>

and you have to just call updatemodel in your action method like

UpdateModel(yourobject);

i have not tested this code but i am more than certain that changing your html this way will get you sorted. @Mlchael Grassman you don't have to use integer based id's to bind collection anymore. plz read steve's post for more information. Actually you don't need GUID either but a random number that is unique in page's context provided that you put an extra hidden field with name index and value set to above mentioned random number. I have blogged about Master detail form in asp.net mvc using (and modifying) BeginCollectionItem method written by Steve. in part one of this blog series i simply showed how you can dynamically add fields of your detail record on client side without ajax call. In second part i brought the editor template of detail record to jquery templating engine and rendering it on client side as it would appear if rendered through ajax call.

更多推荐

本文发布于:2023-07-26 00:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268378.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   层次   结构   MVC   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!