1

I am using ASP.NET MVC Entity Framework and I have a page to insert data

public ActionResult Create()
        {
            return View();
        }

        // POST: /Home/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="id,firstname,lastname,email,guests,guestfirstname,guestlastname,productInterest,occupancyTimeline,isInvestment,timeSlot,dateSlot")] CP_LC_Preview cp_lc_preview)
        {
            if (ModelState.IsValid)
            {
                db.Data.Add(cp_lc_preview);
                db.SaveChanges();
                return RedirectToAction("Confirm", new { info = cp_lc_preview });
            }

            return View(cp_lc_preview);
        }

What I am trying to do is take that data that was just entered and pass it to another controller to display. like a confirmation page.

Here is my method for the confirm page

public ActionResult Confirm()
        {
            return View();
        }
1
  • "pass it" from where? Generally a controller action would accept a model as a method parameter, as you've already done in the second Create() method. How specifically is the user requesting the Confirm() action?
    – David
    Sep 2, 2016 at 15:33

2 Answers 2

3

You may consider following the PRG pattern.

PRG stands for POST - REDIRECT - GET. With this approach,After you successfully save the data, you will issue a redirect response with a unique id in the querystring, using which the second GET action method can query the resource again and return something to the view.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="id,firstname,lastname,email,guests,guestfirstname,guestlastname,productInterest,occupancyTimeline,isInvestment,timeSlot,dateSlot")] CP_LC_Preview cp_lc_preview)
{
    if (ModelState.IsValid)
    {
       db.Data.Add(cp_lc_preview);
       db.SaveChanges();
       var id = cp_lc_preview.Id; 
       return RedirectToAction("Confirm", new { id = id });
    }
   return View(cp_lc_preview);
}

and in your Confirm action method, have id parameter and using the value of that read the record from the db again and use as needed.

public ActionResult Confirm(int id)
{
   var d = db.Data.FirstOrDefault(g=>g.Id==id);
   // Use d as needed   
   // to do : Return something
}

TempData

If you do not prefer to have this id in the url, consider using TempData to pass the data. But TempData has a short life span. Once read, the data is gone. TempData uses Session behind the scene to store the data.

TempData["NewItem"] = cp_lc_preview;
return RedirectToAction("Confirm", "controllerName");

and in the Confirm method

public ActionResult actionname()
{      
  var model=TempData["NewItem"] as CP_LC_Preview 
  // to do : Return something
}

For your reference

How do I include a model with a RedirectToAction?

2

You can use TempData for that.

TempData["YourData"] = YourData;//persist data for next request

var myModel=TempData["YourData"] as YourData //consume it  on the next request

What is TempData ?

  1. TempData is meant to be a very short-lived instance, and you should only use it during the current and the subsequent requests only.

  2. Since TempData works this way, you need to know for sure what the next request will be, and redirecting to another view is the only time you can guarantee this.

  3. Therefore, the only scenario where using TempData will reliably work is when you are redirecting.This is because a redirect kills the current request , then creates a new request on the server to serve the redirected view.

  4. Simply said, Asp.Net MVC TempData dictionary is used to share data between controller actions.

  5. The value of TempData persists until it is read or until the current user’s session times out.

  6. By default, the TempData saves its content to the session state.

  7. TempData values are marked for deletion when you read them. At the end of the request, any marked values are deleted.

  8. The benefit is that if you have a chain of multiple redirections it won’t cause TempData to be emptied the values will still be there until you actually use them, then they clear up after themselves automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.