Recover data redirectaction mvc c #

1

I have a student consultation,

// POST: /Admin/Anuncio
    [HttpPost]
    public ActionResult Index(Estudante estudante)
    {
        if (ModelState.IsValid)
        {
            List<EstudantesEncontrados> list = GetEstudante(GetEstudanteProximo(estudante));

            if (list != null)
            {
                TempData["ListaEstudantesEncontrados"] = list;
                return RedirectToAction("Lista", "Busca");

            }
            else
            {
                ModelState.AddModelError("", "Erro Ao pesquisar");
            }

        }

        // If we got this far, something failed, redisplay form
        return View();

    }

If I find the students redirects to the list of the students found, when I update the page in the student list it gives an error in the view because the TempData that sends the MODEL is coming null from the above action, someone could you help me?

I would like to know if there is any way when updating the browser it will get the data that was sent in the form previously.

public ActionResult Lista()
    {
        ViewBag.Message = "";

        if (ModelState.IsValid)
        {
            if (TempData["ListaEstudantesEncontrados"] != null)
            {
                var model = TempData["ListaEstudantesEncontrados"] as List<EstudantesEncontrados>;
                return View(model); ;
            }
            else
            {
                ModelState.AddModelError("", "Erro ao consultar");
                return RedirectToAction("Index", "Home"});
            }
        }else{
            return RedirectToAction("Index", "Home");
        }           

    }
    
asked by anonymous 28.05.2014 / 07:33

2 answers

2

This code is quite wrong. TempData should not be used to send information from a search to View .

Create a ViewModel for Student:

ViewModels \ StudentViewModel

public class EstudanteViewModel {
    /* Coloque aqui properties que vão corresponder aos parâmetros em tela */

    public virtual IList<Estudante> EstudantesEncontrados {get;set;}
}

On the Controller, modify the following:

// POST: /Anuncios/Index
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
public ActionResult Index(EstudanteViewModel viewModel = null)
{
    if (viewModel != null && ModelState.IsValid)
    {
        var list = GetEstudante(GetEstudanteProximo(estudante));

        if (list != null)
        {
            viewModel.EstudantesEncontrados = list;
            return View(viewModel);
        }
        else
        {
            ModelState.AddModelError("", "Erro Ao pesquisar");
        }

    }

    return View(new EstudantesViewModel());
}

So you send a EstudanteViewModel pleted object to Controller and you can search for it. If this object is not properly filled, it displays% empty%.

Therefore, Action View is no longer required, since you can use the Lista list to display your results on screen.

    
28.05.2014 / 08:08
2

The Cigano warned very well, actually sending your collection to the View using TempData is not the most correct way.

As he suggested, it's interesting, but one important thing to note: "your View needs to be typed, that is, the StudentViewModel type."

TempData is very useful for displaying temporary messages, such as success messages after a registration, for example:

Controller:

public class EstudanteController : Controller
{ 
    [HttpPost]
    public ActionResult Salvar()
    {
         //Código para salvar um Estudante.
         TempData["mensagem"] = "Mensagem de sucesso";
         return RedirectToAction("Index");
    }
}

View: (Index)

@model EstudanteViewModel   

<div>
    @TempData["mensagem"]
</div>

//Código restante...

After being redirected to the Index view, the message in TempData is displayed.

    
28.05.2014 / 19:58