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");
}
});
}
- Change the year drop down will call fn_ChangeYear() --> Report_ChangeYear
- Can read TempData["Name"] value
- Change the year drop down again
- TempData["Name"] is null , Not sure why it is so
Any idea on the above issue?