-1

Im trying to pass data from one controller to another. i have a Pet that i need to parse to a Message controller. Basicly i have a user who creates a pet, then other users can send messages to other pet owners through the WebAPP. on Pets/Details i have a button calling the action SendMessage(shown below)

Currently i get a unhandled exception: InvalidOperationException: Session has not been configured for this application or request. Microsoft.AspNet.Http.Internal.DefaultHttpContext.get_Session()

PetsController.CS:

public IActionResult SendMessage(int id)
        {
            Pet PetData = _context.Pet.Single(m => m.ID == id);
            TempData["PetData"] = PetData;
            return RedirectToAction("Create", "Message");
        }

Then i need to move that pet object to my messagesController:

// POST: Messages/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(Message message)
        {
            if (ModelState.IsValid)
            {
                _context.Message.Add(message);
                Pet Data = TempData["PetData"] as Pet;
                message.SentTo = Data.CreatedBy;
                message.Subject = Data.Name;
                _context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(message);
        }

I hope that someone can help me with the Issue. Just to mention it, im new to MVC.

Yssing
  • 1
  • 3
  • Try researching that error. Which ASP.NET version do you use? – CodeCaster Apr 18 '16 at 09:26
  • I think session state is disabled in your mvc app..if you are using asp.net 5 and mvc 6 then these links will help you.. [link1](http://stackoverflow.com/questions/32250659/using-sessions-in-asp-net-5-mvc-6), [link2](http://stackoverflow.com/questions/25077298/how-to-implement-session-state-in-asp-net-vnext-mvc-6) – Kartikeya Khosla Apr 18 '16 at 09:29
  • i use ASP.NET Version 5. – Yssing Apr 18 '16 at 09:30
  • Probable duplicate - http://stackoverflow.com/questions/33814042/using-tempdata-is-crashing-my-application – Yogi Apr 18 '16 at 09:37
  • Thx Kartikeya, i got past the exception the error was that i did not declare the session dependencies proper. but now i get: A database operation failed while processing the request. InvalidOperationException: sequence does not contain any elements. i suspect that it might be the TempData method im using incorectly. – Yssing Apr 18 '16 at 09:58

1 Answers1

0

The TempData method returns single values only, my error was that i tried to send the TempData["PetData"] into a Pet object therefore it returned null since it only returns a single value and not the hole object.

Fix: added to Project.JSON, restored pakages

"Microsoft.AspNet.Session": "1.0.0-*" 

Added to startup.cs ConfigureServices

services.AddSession(); 
services.AddCaching();

Then i reformed the method i used to send the user to /Messages/create

public IActionResult SendMessage(int id)
{
    Pet PetData = _context.Pet.Single(m => m.ID == id);

    TempData["PetOwner"] = PetData.CreatedBy;
    TempData["PetName"] = PetData.Name;
    return Redirect("/Messages/Create");
}

After that i realized that i tried to sent the Data into a Pet Obejct and then the DB returned Null Not Allowed.

I then reformed the Create Message Method

 // POST: Messages/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(Message message)
        {
            if (ModelState.IsValid)
            {
                _context.Message.Add(message);
                string PetOwner = TempData["PetOwner"].ToString();
                string PetName = TempData["PetName"].ToString();
                message.SentTo = PetOwner;
                message.Subject = PetName;
                message.DateSent = System.DateTime.Now;
                message.SentBy = User.Identity.Name;
                message.Read = false;
                _context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(message);
        }

Thanks to Kartikeya Khosla for pointing me in the right direction.

Yssing
  • 1
  • 3