2

Code

I can not send an array of javascript objects to the server. ModelBinder standard does not recognize the format.

On my server I have classes:

public class PessoaViewModel
{
    [HiddenInput(DisplayValue = false)]
    public int Id { get; set; }

    public string Nome { get; set; }
    public string Tipo { get; set; }
    public string CPF { get; set; }

    public ICollection<TelefoneViewModel> Telefones { get; set; }
    public ICollection<EnderecoViewModel> Enderecos { get; set; }
    public ICollection<EmailViewModel> Emails { get; set; }
    public PessoaViewModel Conjuge { get; set; }
}

public class TelefoneViewModel
{
    [HiddenInput(DisplayValue = false)]
    public int Id { get; set; }

    [AdditionalMetadata("class", "span2")]
    [AdditionalMetadata("placeholder", "Tipo")]
    public string Tipo { get; set; }

    [DataType(DataType.PhoneNumber)]
    [AdditionalMetadata("class", "span2")]
    public string Numero { get; set; }

    [HiddenInput(DisplayValue = false)]
    public int Ordem { get; set; }
}

I'm not listing the rest of the class because it is not yet used in the code!

My javascript

$.ajax
    url:  $(form).attr("action")
    type: "POST"
    error: (err, errType, errMessage) -> 
        console.error a.statusText
    cache: false
    data: ko.mapping.toJS(@, ignoreFunctionsMapping) 
    success: (data, txtStatus) -> console.log "OK!!?? On server maybe not!"

Server result

Action binder

Full image: https://i.sstatic.net/NeIm1.jpg

Question

As you can see, the post is being done and the values ​​are correct. But asp.net mvc does not fill correctly the Telefones object!

What am I doing wrong?

3
  • This might be of interest: stackoverflow.com/a/11182113/1373170 The default model binder uses the following naming convention for collections: Telefonos[0].Numero, which is slightly different than the one you are using. Commented Aug 28, 2012 at 0:54
  • The problem is that this convention was generated by jquery or by the browser (I can not say for sure) when I make the post. Remember that I am sending the data as javascript object and not the conventional way would be $("form").serialize() Commented Aug 28, 2012 at 12:51
  • Did you try using submit() just to check how it behaves ? I am having the same problem =/ Commented Jun 8, 2015 at 8:17

1 Answer 1

1

Your problem is because you are missing a ".", it should have the format:

Telefones[0].Id: 1
Telefones[0].Ordem: bli
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.