Web Application ASP MVC Core 2.1 I have two models with a Parent-Children relationship, they are Course and CourseEdition; a Course can have many Editions.
The page where I display Course details also shows a list of its editions. I use JQuery to populate the same page with the details of the edition: when clicking on one of the editions I have a DIV filled with its details data. There are actions (i.e. enroll one or more person in an edition) that I would like to end with a redirect to the Editions details.
I have set up a mechanism like this:
In the controller action "RegisterPeople" I set TempData with the ID of the Edition I want to be redirected to, then I redirect to the Details action in CourseController.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> RegisterEmployees([Bind("CourseId,CourseEditionId,Subscribers")] RegistrationData regData)
{
TempData["NextCourseId"] = regData.CourseId;
TempData["NextEditionId"] = regData.CourseEditionId;
if (ModelState.IsValid)
{
<do something>
return RedirectToAction("Details", "Courses", new { id = regData.CourseId });
}
Here (Course Controller, Details Action) I read TempData and copy its value in ViewBag.
public async Task<IActionResult> Details(int? id)
{
<do something>
ViewBag.GotoEditionId = TempData["NextEditionId"] ;
return View();
}
In the details view I have a DIV with a data-attribute populated by the ViewBag value (the Edition's ID),
<div id="GotoEditionId" data-ceid="@ViewBag.GotoEditionId"> </div>
@section Scripts {
@{await Html.RenderPartialAsync("_CourseScripts");}
}
Now, in _CourseScript.cshtml a JQuery event handler is triggered when clicking on this div does the magic of populating the edition div.
$(document).ready(function () {
$(document).on("click", "#GotoEditionId", function (e) {
e.preventDefault();
var params = {};
params["id"] = $(this).data("ceid");
$.ajax({
url: $("#GetEditionUrl").data('get-edition'),
type: "GET",
data: params,
success: function (result) {
$("#EditionBlock").html(result);
},
error: function (err) {
alert("error");
$("#EditionBlock").html(err.responseText);
}
});
});
var GotoEditionId = $("#GotoEditionId").data("ceid");
if (GotoEditionId > 0) {
$("#GotoEditionId").trigger("click");
}
})
This works perfectly while I am in development with Visual Studio. When I deploy to a remote IIS server, TempData is filled in the "register" action but it is found empty when entering in the "Details" action.
Who is the culprit? Why TempData is persisted in dev mode and vanishes in production?
EDIT 1
Using Session
In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddSessionStateTempDataProvider()
.AddJsonOptions(config =>
{
config.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
services.AddSession();
.....
public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env)
{
....
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();