简体   繁体   English

在EditorTemplate(在数组上)/多个绑定前缀中发布表单期间绑定model?

[英]Bind model during post of form inside EditorTemplate (on array)/multiple bind prefixes?

I have a complex Model containing an array.我有一个复杂的 Model 包含一个数组。 For rendering items for this array I'm using EditorFor like this:为了渲染这个数组的项目,我使用 EditorFor 像这样:

for (int i = 0; i < Model.Contacts.Phones.Length; i++)
{       
    @Html.EditorFor(x => x.Contacts.Phones[i])
}

Inside editor there is a post-form.在编辑器里面有一个post-form。 Problem is, that binding is successful only when I exactly specify binding prefix:问题是,只有当我准确指定绑定前缀时,绑定才会成功:

[HttpPost]
public ActionResult SavePhone(
    [Bind(Prefix = "Contacts.Phones[0]")]Contact5UsedPhone model)
{ ... }

So it works only for first of the elements.所以它只适用于第一个元素。 What is the correct form of prefix?前缀的正确形式是什么?

For the more there is also on the same page editor for different property - but same type of model and same action is therefore executed.此外,在同一页面编辑器上还有不同属性的 - 但相同类型的 model 并因此执行相同的操作。 Is it possible to set more than one binding prefix?是否可以设置多个绑定前缀? Eg例如

[HttpPost]
public ActionResult SavePhone(
    [Bind(Prefix = "Contacts.Phones[0], Contacts.AnotherPrefix")]
    Contact5UsedPhone model)
{ ... }

Thank you!谢谢!

edit - model:编辑-model:

public class ContactsViewModel
{
    public Contact5UsedPhone AddiblePhone {get;set;}
    public Contact5UsedPhone[] Phones {get;set;}
    ...
}

edit - answer: I found a solution for this.编辑 - 答案:我找到了解决方案。 As there is one array (Phones) and one single entity (AddiblePhone), I used two parameters and simple if:由于有一个数组(Phones)和一个单一实体(AddiblePhone),我使用了两个参数,如果:

[HttpPost]
public ActionResult SavePhone(
    [Bind(Prefix = "Contacts.Phones")]Contact5UsedPhone[] models, 
    [Bind(Prefix = "Contacts.AddiblePhone")]Contact5UsedPhone model)
{
    model = model ?? models[0];
    ...
}

The question still remains - what if there were AddiblePhones as array?问题仍然存在——如果有 AddiblePhones 作为数组呢? Is it possible to use two prefixes for one parameter, or does it have to be divided to two parameters as I did in this case?是否可以为一个参数使用两个前缀,还是必须像我在这种情况下那样将其分为两个参数?

We found clear and simple answer for this:我们为此找到了明确而简单的答案:

@Html.EditorFor(x => x.Phones[i], 
    "~/Views/Contacts/EditorTemplates/Contact5UsedPhone.cshtml", 
    "")

The last "" means it won't use any prefix for binding.最后一个“”表示它不会使用任何前缀进行绑定。 That's great, so you don't need any binding prefix and two kinds of accepted modes as showed in answer in the question.太好了,因此您不需要任何绑定前缀和两种接受的模式,如问题的答案所示。

A little question still remains - what if there were AddiblePhones as array?还有一个小问题——如果有 AddiblePhones 作为数组呢? Is it possible to use two prefixes for one parameter, or does it have to be divided as I proposed in answer in question?是否可以为一个参数使用两个前缀,还是必须按照我在问题答案中提出的那样进行划分? But probably this signals bad design if something like that is needed...但是,如果需要这样的东西,这可能表明设计不好......

EDIT (Telerik controls): Problem of this nice solution appears when using Telerik dropdown list, as it generates not unique ids for elements and these controls are jQuery controlled, therefore it doesn't work properly.编辑(Telerik 控件):使用 Telerik 下拉列表时会出现这个很好的解决方案的问题,因为它为元素生成不唯一的 ID,并且这些控件受 jQuery 控制,因此无法正常工作。

WARNING: I found out, that when using Bind attribute you CANNOT use " " (space) between attribute and type of parameter.警告:我发现,当使用 Bind 属性时,您不能在属性和参数类型之间使用“”(空格)。

works:作品:

[Bind(Prefix = "Phones")]Contact5UsedPhone[]

doesn't work:不起作用:

[Bind(Prefix = "Phones")] Contact5UsedPhone[]

I don't know if this is only case of arrays.我不知道这是否只是 arrays 的情况。 But it seems wird to me.但这对我来说似乎很奇怪。

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

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