Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, November 15, 2011 9:13 AM
I am trying to access controller ViewData["done"] in javascript my code is
var str = <%= (Boolean)ViewData["done"] ? "true" : "false" %>;
but this always gives me error Null Reference exception, Can any one tell me what is the problem.
Thanks
All replies (10)
Wednesday, November 16, 2011 9:58 AM âś…Answered
Thanks guys i got the solution, What i did is i made a async= true Ajax call and return the TempData value in javascript and get it done.
Tuesday, November 15, 2011 9:19 AM
REfer the link
http://stackoverflow.com/questions/5863094/asp-net-mvc-using-viewdata-in-javascript
http://stackoverflow.com/questions/5528815/problem-in-acessing-viewdata-in-javascript
Tuesday, November 15, 2011 9:29 AM
I tried all of there but still error is coming.
Tuesday, November 15, 2011 9:30 AM
Can you try with tempdata instead of view data?
Tuesday, November 15, 2011 10:03 AM
Same error i think i need to handle null reference but i do not know how i will do that in javascript.
Tuesday, November 15, 2011 11:31 AM
the null reference is in the server view code, not javascript. try:
var str = <%= ((bool?)ViewData["done"]?? false) ? "true" : "false" %>;
Tuesday, November 15, 2011 11:55 AM
now object reference error not coming, since i am assigning variable on save complete in controller and code is executing but seems value not setting and in javascript its always showing false.
my code is
function Grid_OnComplete(e) {
if (e.name == "submitChanges") {
var str = <%= ((bool?)ViewData["done"]?? false) ? "true" : "false" %>;
alert(str);
if (str == true) {
alert("Saved");
}
}
}
Tuesday, November 15, 2011 1:32 PM
thats because there is no value in ViewData["done"], thats the case the modified code handles. you need to set a ViewData["done"] = true (or ViewBag.done = true) in the controller before the view is called.
Tuesday, November 15, 2011 3:00 PM
bruce i set this value ViewData["done"] = true but view is already loaded and when my update is completed then i am setting this
ViewData["done"] = true
Tuesday, November 15, 2011 7:33 PM
this is no valid reason to update ViewData after your controller calls return View(). its a coding error. please explain what you are doing.