Recover Data Redirectaction mvc#

Asked

Viewed 921 times

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 you find students redirects to the list of students found, when I refresh the page in the list of students it gives an error in the view because the Tempdata["Student Listfound"] that sends the MODEL is coming null from the above action, someone could help me?

I wonder if there is any way when it is updated the browser it takes 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");
        }           

    }
  • Lisbão, I believe that the error ta in the view, please post your view?

2 answers

2

The Gypsy warned very well, really send your collection to the View using Tempdata is not the right way.

As he suggested, it is interesting, but an important observation: "your View needs to be typed, that is, of the type Student".

Tempdata is quite useful to display temporary messages such as success messages after a registration, for example:

Controler:

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 Tempdata message is displayed.

2

This code is very wrong. TempData should not be used to send information from a survey to the View.

Create a ViewModel for the Student:

Viewmodels Estudanteviewmodel

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

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

In 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 an object EstudanteViewModel completed for the Controller and can search for it. If this object is not properly filled in, it displays a View empty.

Thus, the Action Lista is no longer required, since you can use the ViewModel to display your results on screen.

Browser other questions tagged

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