形式门柱内侧EditorTemplate(在数组)中绑定模型/多种绑定prefixes? [英] Bind model during post of form inside EditorTemplate (on array)/multiple bind prefixes?

查看:157
本文介绍了形式门柱内侧EditorTemplate(在数组)中绑定模型/多种绑定prefixes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含数组的复杂的模型。渲染项这阵我使用EditorFor是这样的:

I have a complex Model containing an array. For rendering items for this array I'm using EditorFor like this:

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

编辑器里面有一个后形式。问题是,绑定成功,只有当我正好指定绑定preFIX:

Inside editor there is a 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)
{ ... }

因此​​,它仅适用于第一个元素。什么是preFIX的正确形式?

So it works only for first of the elements. What is the correct form of prefix?

有关更多的也有在同一页面编辑器不同的属性 - 但因此执行相同类型的模型,同样的动作。是否有可能设置多个绑定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. Is it possible to set more than one binding prefix? E.g.

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

感谢您!

编辑 - 型号:

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

编辑 - 回答:
我发现了一个解决方案。由于有一个阵列(手机)和一个单一的实体(AddiblePhone),我用了两个参数,如果简单:

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:

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

问题仍然存在 - 如果有什么有AddiblePhones为数组?是否有可能使用两个prefixes一个参数,还是有我在这种情况下,没有被划分到两个参数?

The question still remains - what if there were AddiblePhones as array? 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", 
    "")

最后一个,意味着它不会使用任何preFIX绑定。这是伟大的,所以你不需要任何约束力preFIX和两种模式被接受为在问题答案显示。

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.

一个小问题仍然存在 - 如果有什么有AddiblePhones为数组?是否有可能使用两个prefixes一个参数,还是有我在回答问题的拟划分?但也许这预示糟糕的设计是否需要类似的东西...

A little question still remains - what if there were AddiblePhones as array? 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...

编辑(Telerik控制):
当使用Telerik的下拉列表中,因为它会产生不唯一ID的元素和这些控件是jQuery的控制,因此它不会正常工作,这会出现很好的解决方案的问题。

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.

警告:
我发现,在使用时绑定参数的属性和类型之间的属性,你不能用(空格)。

WARNING: I found out, that when using Bind attribute you CANNOT use " " (space) between attribute and type of parameter.

作品:

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

不工作:

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

我不知道这是阵列的唯一一例。但似乎wird给我。

I don't know if this is only case of arrays. But it seems wird to me.

这篇关于形式门柱内侧EditorTemplate(在数组)中绑定模型/多种绑定prefixes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆