an exception of type 'system.nullreferenceexception' occurred in driver.dll (in TempData) [duplicate]

1

Why is my error? The error arises in the About action method, just in the ViewBag line.Message = TempData ["data"]. ToString ();

The error arises in the About action method, just in the TempData ["data"]. ToString ();

public ActionResult Index()
    {
        ViewBag.Mensaje = "Bienvenidos al Curso de ASP.NET MVC";
        ViewBag.Profesor = "Carlos Garcia";
        ViewBag.Alumno = "Juan Perez";
    //TempData["nombre"]=valor =>permite compartir entre metodos de accion
     TempData["datos"] = "Profesor:" + ViewBag.Profesor + " y Alumno:" + ViewBag.Alumno;

        return View();
    }

    //------------------------------------------------------


    public ActionResult About()
    {
        ViewBag.Mensaje = TempData["datos"].ToString();
        //ViewBag => Variable Global que permite almacenar valores
        //para ser usados en las vistas ,es un objeto especial que nos permite enviar informacion desde el action hasta la vista
        //ViewBag.NombredeVariable = valor
        ViewBag.Message = "Descripcion de la Pagina Web MVC.";

        return View();
    }
    
asked by Christian Nunton Mantilla 29.01.2018 в 05:27
source

1 answer

0

The problem is that the ViewBag is valid for the current request, that is, when you are filling it in the Index view and navigate to another view, the ViewBag will be empty again. If you want a variable that is valid during the session, use Session . In your Indexer Index you could do something like:

System.Web.HttpContext.Current.Session["datos"] = "Profesor:" + ViewBag.Profesor + " y Alumno:" + ViewBag.Alumno;

And in the About method:

ViewBag.Mensaje = System.Web.HttpContext.Current.Session["datos"].ToString();

What is stored in Session will be valid during the user's session. For another user, the object will be empty.

    
answered by 29.01.2018 / 15:12
source