ASP.Net core TempData.Keep() not working on AJAX call

68 Views Asked by At

I am getting TempData values as null on the second request. Pls help me on this.

Startup

public void ConfigureServices(IServiceCollection services)
{
.................
...............

services.AddControllersWithViews().AddSessionStateTempDataProvider();
services.AddSession();
...........
.............

}

Controller

[HttpGet]
public IActionResult InitializeReport(){
.......................
.......................

 TempData["Name"] = "Hi Dude";
 TempData.Keep("Name");

.......................
.......................
}

//Drop down change call by AJAX

[HttpGet]
public JsonResult Report_ChangeYear(int year, int month){
.......................
.......................

  string sName = TempData["Name"];
  TempData.Keep("Name");

.......................
.......................
}

AJAX from cshtml

function fn_ChangeYear() {
            var SelFromMonth = $("#SelFromMonth");
            var SelToMonth = $("#SelToMonth");
            var yearVal = $("#selYear").val();
            var monthVal = 1;

            $.ajax({
                url: '@Url.Action("Report_ChangeYear", "Home")?year=' + yearVal + '&month=' + monthVal,
                type: 'GET',
                data: '',
                contentType: 'application/json; charset=utf-8',
                success: function (r) {
                    console.log("Suceess");
                    console.log(r);

                    SelFromMonth.empty();
                    SelFromMonth.find('option').remove()

                   $.each(r.months, function (index, itm) {
                       SelFromMonth.append('<option value="' + itm.value + '">' + itm.text + '</option>');
                  });

                },
                error: function (data) {
                    console.log("ERROR");
                    
                }
            });
        }
  1. Change the year drop down will call fn_ChangeYear() --> Report_ChangeYear
  2. Can read TempData["Name"] value
  3. Change the year drop down again
  4. TempData["Name"] is null , Not sure why it is so

Any idea on the above issue?

0

There are 0 best solutions below