Bind model during post of form inside EditorTempla

2019-09-04 18:50发布

问题:

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])
}

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)
{ ... }

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. 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)
{ ... }

Thank you!

edit - 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:

[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? 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?

回答1:

it's nice that u have found a clear solution. Maybe you will like a clearer one. Have a look at
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
http://zahidadeel.blogspot.com/2011/05/master-detail-form-in-aspnet-mvc-3-ii.html
http://ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1/



回答2:

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? 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.

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

works:

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

doesn't work:

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

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