How to pass a parameter together with the Model to a Partialview when rendering it

Asked

Viewed 70 times

1

To create some fields dynamically, I’m using a render and render. When calling Partial _Contact, I would like to pass the [i] value of the for index to sort the fields. It is not working... You’re making a mistake on the page... does anyone know how to help me? A hug!

Error: inserir a descrição da imagem aqui

Generating the fields:

@for (int i = 0; i < Model.PessoasContatosViewModel.Count; i++)
{
   @await Html.PartialAsync("_Contato", Model.PessoasContatosViewModel[i], new ViewDataDictionary { { "indice", i } })
}

Receiving the Model and Parameter in Partialview:

@model Retaguarda.Application.ViewModels.Pessoa.PessoaContatoViewModel
@{
    string valuePassedIn = this.ViewData.ContainsKey("indice") ? this.ViewData["indice"].ToString() : "0";
}

1 answer

1


Change:

@await Html.PartialAsync("_Contato", Model.PessoasContatosViewModel[i], new ViewDataDictionary { { "indice", i } })

For:

   @await Html.PartialAsync("_Contato", Model.PessoasContatosViewModel[i], new ViewDataDictionary(ViewData){ { "indice", i } })

  • 1

    you are the guy!!!! Thank you very, very much :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.