How to pass a parameter from one Action to another Action on the same Controller

2

I need to send a parameter of an action to another action on the same controller, but the parameter is "zeroed". How can I do this? Below the code I'm doing:

[Authorize]
public class ItemMaloteController : Controller
{
    public IActionResult Index(Guid id)
    {
        ViewBag.ItemsMaloteList = _itemMaloteRepository.GetAll()
                                       .Where(x => x.MaloteId.Equals(id)).ToList();

        ViewBag.Malote = _maloteRepository.GetAll()
                         .Where(x => x.Id.Equals(id)).ToList();

        ViewBag.Curso = _cursoRepository.GetAll().ToList();
        ViewBag.Documentos = _documentosRepository.GetAll();
        ViewBag.MaloteId = id;

        return View();
     }

     public IActionResult Create(ItemMalote itemMalote)
        {
            if (!ModelState.IsValid)
                return NotFound();

            itemMalote.Id = Guid.NewGuid();            
            _itemMaloteRepository.Register(itemMalote);
            _itemMaloteRepository.SaveChanges();

            var maloteId = _itemMaloteRepository.GetMalote(itemMalote.MaloteId).Select(x => x.MaloteId).ToList();            

            //Aqui eu chamo minha action "Index" e passo o parametro
            return RedirectToAction("Index", maloteId);
        }
}

It happens that when it arrives again in the action "Index" the parameter arrives "zeroed". I have tried to pass through TempData, but the same thing happens.

    
asked by anonymous 19.11.2018 / 04:59

2 answers

2
return RedirectToAction("Index", new { id = maloteId });

With the above solution given by Peter you will have this as a redirect:

  

/ Controller / Index / maloteId

But there are more overloads for the RedirectToAction . If you need to redirect to another controller's action, for example, you can use this:

return RedirectToAction("MinhaAction", "MinhaController", id);

But on this SOen issue I have seen that it is still possible to not be redirected and you can also use it in this way, using RouteValueDictionary :

return RedirectToAction("SuaAction", new RouteValueDictionary(
       new { controller = "SuaController", action = "SuaAction", Id = id }));

Or you can just do this too:

return Index(id);
    
19.11.2018 / 12:41
1

Create an anonymous type with the id parameter that your Action Index is expecting:

//Aqui eu chamo minha action "Index" e passo o parametro
return RedirectToAction("Index", new { id = maloteId });
    
19.11.2018 / 11:54