7
Answers

How to Read Tempdata, Viewbag to external JS file using ajax

ashok kumar

ashok kumar

9y
1.7k
1
How to Read Tempdata, Viewbag in external JS file using ajax jquery method
 
  I have logon controller with method
 
public ActionResult Index()
{
      TempData["Ex"] = "True";
      return View();
}
 
i called a request by button's click event and click event code is in external js file as given 
 
var data = $('form').serialize();
$.ajax({
            url: "../RP/GetDetails", // RP is another controller
            dataType: 'html',
            t ype: "GET",
            data: data,
            contentType: "application/html; charset=utf-8",
            success: function (data)
            {
                     if ('@Html.Raw(TempData["Ex"])' == 'True') // Need to access tempdata value here
                           { 
                                  / / Perform some action
                           }
                     else
                           {
                                  // Else perform some other action
                           }
            },
            error: function (error)
               {
                        alert("Please Check Model..!!");
               }
   });
 
In this case, Method of report controller is called on button click event but if session is expire then automatically login controller is called and tempdata
value is set based on that tempdata value in external js file, action will be prosessing but i am unable to get  TempData["Ex"]  js file.
Answers (7)
0
Vignesh Mani

Vignesh Mani

NA 13.4k 938.4k 9y
Hi TempData act as like string. If you store value that value also act as a string.
0
ashok kumar

ashok kumar

NA 32 0 9y
 
 I have tried this but not working this will behave like string and not give value. 
0
Vignesh Mani

Vignesh Mani

NA 13.4k 938.4k 9y
Hi Just use in jquery like '@TempData["Ex"]' instead of raw data. Just try this. otherwise inform me i will try another method.
0
ashok kumar

ashok kumar

NA 32 0 9y
Thanks Vignesh Mani,
 
  But Data param in success function returning view. so i can not do this. actually based on Tempdata["Ex"] value i am trying to identifying which view is returning in data param and based on that i am modifinng DOM.
0
Vignesh Mani

Vignesh Mani

NA 13.4k 938.4k 9y
Hi Friend try below mentioned method. Please modify in your code what i wrote in green color
 
public ActionResult Index()
{
return Json(new { status = "Success", message = "Success" });  
}
 
 
var data = $('form').serialize();
$.ajax({
url: "../RP/GetDetails", // RP is another controller
dataType: 'html',
t ype: "GET",
data: data,
contentType: "application/html; charset=utf-8",
success: function (data.status == "Success")
{
if ('@Html.Raw(TempData["Ex"])' == 'True') // Need to access tempdata value here
{
/ / Perform some action
}
else
{
// Else perform some other action
}
 
 
},
error: function (error)
{
alert("Please Check Model..!!");
}
});
 
 
0
ashok kumar

ashok kumar

NA 32 0 9y
Thanks Shuvojit,
 
 
   But i have already tried this code. this is not working. If i write my button click ajax code to view and use to read temp data value as 
 
if('@Html.Raw(TempData["Ex"])'==true)
 
then this is working but when i put this code in external js this not working. 
0
Shuvojit Halder

Shuvojit Halder

NA 1k 10.4k 9y
use this if ('TempData["Ex"]' == true)