Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
Problems passing parameter to an action

I'm trying to pass data to an action inside the same controller. I'm using RedirectToAction, but I'm not succeeding. The action is called but the data I'm trying to pass to it has its values ​​null.

My Model:

public class PlaylistModel
{
    public Guid PlayListID { get; set; }
    [Display(Name = "Nome da Playlist")]
    public string NomePlayList { get; set; }
    [Display(Name = "Descrição")]
    public string Descricao { get; set; }
    [Display(Name = "Código")]
    public string CodigoPlayList { get; set; }
    public string Estado { get; set; }
    public string EmailUser { get; set; }
    public List<VideoThumbnails> VideosIdsYoutube { get; set; }
    [Display(Name = "Categorias")]
    public List<string> Categorias { get; set; }
}

This action receives the data, does the processing and calls the other action

[HttpPost]
public ActionResult CreatePlaylist(string Categorias, string NomePlayList)
{
    PlaylistModel playListModel = new PlaylistModel();
    playListModel.VideosIdsYoutube = AppCache.Instance.VideoParaAPI.VideoThumbnails.ToList();
    playListModel.Categorias = Categorias.Split(',').ToList();
    playListModel.NomePlayList = NomePlayList;
    playListModel.Estado = EnumEstadoDaPlayList.Nova.ToString();
    playListModel.EmailUser = AppUser.User.Email;

    var api = new AccessAPI.Playlist.AccessAPIPlaylist();
    var playlist = api.CriarNova(playListModel);

    if(playlist != null)
    {
        return RedirectToAction("ExibirPlaylist", new RouteValueDictionary(new { controller = "Playlist", action = "ExibirPlaylist", pPlayList = playlist }));
    }
    else
    {
        return Problem("Não foi possível criar a playlist!");
    }
}

This other action is called by the previous one, but the model arrives with null values

[HttpGet]
public ActionResult ExibirPlaylist(PlaylistModel pPlayList)
{
    var apiVideo = new AccessAPI.Video.AccessAPIVideo();
    var videos = apiVideo.ConsultarPorPlayListId(pPlayList.PlayListID).ToList();
    if(videos.Count > 0)
    {
        videos.ForEach(v => 
        {
            pPlayList.VideosIdsYoutube.Add(new Dommain.Cache.VideoThumbnails() { CanalID = v.CanalID, DataDePublicacao = v.DataDePublicacao, NomeDoCanal = v.NomeDoCanal, NomeDoVideo = v.NomeDoVideo, Thumbnail = v.Thumbnail, VideoId = v.VideoIdYoutube});
        });
    }
    return View();
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The RouteValueDictionary in RedirectToAction() pass only simple types, like string, int etc. The RedirectToAction() doesn't serialize the complex data types. Therefore, may be the easiest way to use the TempData.

In the CreatePlaylist(string Categorias, string NomePlayList):

TempData["playListModel"] = playlist;
return RedirectToAction("ExibirPlaylist");

And then:

[HttpGet]
public ActionResult ExibirPlaylist()
{
    if (TempData["playListModel"] is PlaylistModel pPlayList)
    {
       // Your code using `pPlayList`
    }
    return View("Index");
}

The MVC uses the binary serialization, by default. And in some cases the binary serialization can fail: Storing objects (non-string things) in TempDataDictionary does not round trip correctly. Therefore, it is preferably to serialize the specified object to a JSON string by JsonConvert.SerializeObject().

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda