question

arsar-2618 avatar image
0 Votes"
arsar-2618 asked YijingSun-MSFT answered

pass data to another page after successfull insert

After saving my data to database I want to display the information entered by the user in another page (`printRegInfo.cshtml`). How can I do that?



 public ActionResult Create(FirmServiceRegistrationViewModel firmServiceReg)
     {
         try
         {
             ViewBag.ServiceId = new SelectList(db.ServicesModels, "ServiceId", "ServiceName");
             if (ModelState.IsValid)
             {
                 FirmsModel frm_ = new FirmsModel();
                 frm_.Name = firmServiceReg.Name;                    
                 frm_.PropName = firmServiceReg.PropName;
                 frm_.Address = firmServiceReg.Address;
                 db.FirmsModels.Add(frm_);
                 db.SaveChanges();
                 int frmId = frm_.FirmId;
    
                 FirmServiceRegistrationModel frmServReg = new FirmServiceRegistrationModel();
                 frmServReg.ServiceId_ = firmServiceReg.ServiceId;
                 frmServReg.FirmId_ = frmId;
    
                 db.FirmServiceRegistrationModels.Add(frmServReg);
                 db.SaveChanges();
             }                
             return View();
         }
         catch (Exception)
         {
             throw;
         }
     }

Now on successful insert of data I want to pass the data entered by the user [Name, PropName, Address] to the view page say printRegInfo.cshtml. How can I do that?




dotnet-aspnet-generaldotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @arsar-2618 ,
I suggest you could use Tempdata for passing data.The main advantage of TempData is passing data between different controllers when redirect happens it retains data between controller to controller. Just like this:

 TempData["FirmRegData"] = frm_;
 return RedirectToAction("/Controller/Action");

You could do like this:

 public ActionResult Create(FirmServiceRegistrationViewModel firmServiceReg)
 {    
 try    
 {    
          ViewBag.ServiceId = new SelectList(db.ServicesModels, "ServiceId", "ServiceName");    
          if (ModelState.IsValid)    
          {    
             ...    
             TempData["FirmRegData"] = frm_;    
           }    
             return Redirect("/Home/printRegInfo");    
      }   
      catch (Exception)    
      {    
          throw;    
      }    
 }
    
  //other page    
 public ActionResult printRegInfo()    
 {    
     try
     {   
          FirmsModel frm_ = (FirmsModel)TempData["FirmRegData"];    
          return View(frm_); 
      }    
 catch (Exception)    
     {   
         throw;    
     }    
 }

Best regards,
Yijing Sun


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

Just redirect to the page passing the record Id. The page can lookup the record and display the data.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.