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?
CodePudding user response:
The below code just get entered info and carried to another page using temp data. Hence you need to cast the as per model data and populate in view.
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();
TempData["FirmRegData"]=frm_;
}
return RedirectToAction("printRegInfo","ControllerName");
}
catch (Exception)
{
throw;
}
}
public ActionResult printRegInfo()
{
try
{
FirmsModel frm_ =(FirmsModel)TempData["FirmRegData"];
// Here you will get all entered info.
}
return view(frm_);
}
catch (Exception)
{
throw;
}
}