0
$("#@ViewBag.PageGroupID").append(data);

Now ViewBag.PageGroupID returns the id of the div at runtime.What if i want to append data in the div(the id which i will get runtime).How do i acheive this?

$('#btnPageElementClick').click(function () { 
   var flag = false; 
   var b; 
   $.ajax({ type: 'GET', url: '/ScriptedTestCase/pageElementPV/' +     $('#PageElements').val(), data: null, 
    success: function (data) 
    { 
       $("#@ViewBag.PageGroupID").append(data); //where divID is the id of the div you append the data. 
    } 
   }); 
return false; 
});
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
Soni
  • 3
  • 2
  • 1
    the append method should work fine. Do you have some valid markup in the data variable ? where are you calling this ? – Shyju Aug 14 '12 at 15:22
  • In what context you are using this WONDERFUL ViewBag ? There should be a better way to handle this. Tell us more about the context – Shyju Aug 14 '12 at 15:25
  • $("#divID).append(data) works fine, but in the above case i am not able to append data on the ViewBag value. i think there could be syntax error in it which i am unable to figure out – Soni Aug 14 '12 at 15:27
  • Are you sure the PageGroupID return the correct ID? Also, what does the HTML output? Maybe it output the append text just not visually like you would like but still output in the Html code. – Patrick Desjardins Aug 14 '12 at 15:28
  • Is this line of code inside – Patrick Desjardins Aug 14 '12 at 15:29
  • @soni: Do you want to update the Viewbag data ? or simply update the content of an element in your DOM which has an id same as the ViewBAd data ? – Shyju Aug 14 '12 at 15:37
  • @Shyju - I need to update an element in the DOM which has the id same as the Viewbag data... Now the question here is that in jquery i cannot access the ViewBag's value – Soni Aug 14 '12 at 15:39
  • @Soni: Is this code inside a razor page ? – Shyju Aug 14 '12 at 15:41
  • @Shjyu - yes it is inside a razor page and inside the script tag – Soni Aug 14 '12 at 15:42
  • $('#btnPageElementClick').click(function () { var flag = false; var b; $.ajax({ type: 'GET', url: '/ScriptedTestCase/pageElementPV/' + $('#PageElements').val(), data: null, success: function (data) { $("#@ViewBag.PageGroupID").append(data); //where divID is the id of the div you append the data. } }); return false; }); – Soni Aug 14 '12 at 15:43
  • @Soni: Please do not post such code in comments. Update the question and add it. Did you check the Value in ViewBag before accessing it ? `alert("@ViewBag.PageGroupID")` – Shyju Aug 14 '12 at 15:46
  • sorry for doing so as i am doing it for the first time.. yes i did check the value of alert("@ViewBag.PageGroupID").Its unable to retrieve the value.i cannot access it. – Soni Aug 14 '12 at 15:48

1 Answers1

0

Make sure that inside the controller action that rendered the view that contains the script you have shown, you have actually set this data:

ViewBag.PageGroupID = "someDivId";

Also note that in HTML ids cannot start with numbers.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • yes i am setting a value for the ViewBag in the controller.. But when i try to access the value in the view alert(ViewBag.PageGroupID). it does not display any value – Soni Aug 14 '12 at 16:26
  • You need to access it like this: `alert('@(ViewBag.PageGroupID)');`. If this doesn't display anything it means that you haven't properly set the value in your controller action that rendered this view. – Darin Dimitrov Aug 14 '12 at 16:26
  • i cannot view its value,i get a blank alert box although i have set its value in the controller – Soni Aug 14 '12 at 16:35
  • That's impossible. You probably set the value in the wrong controller action, not the one that rendered this view. Unfortunately without seeing your code that's pretty hard to tell. We can only be guessing here. – Darin Dimitrov Aug 14 '12 at 16:39
  • if i set ViewBag's value in Index view i can see that value in the alert but if i set the ViewBag's value in another action,i can see its value while debugging,but i cannot see its value in the alert box – Soni Aug 14 '12 at 17:17
  • was just going through this link http://stackoverflow.com/questions/6858723/lifetime-of-viewbag-elements-in-asp-net-mvc3.After setting a value in the Viewbag i am calling a Partial view,this might be the case that the value in the ViewBag gets flushed – Soni Aug 14 '12 at 21:15
  • Yes, you set the ViewBag inside the controller action and you can only use it inside the view that was rendered by this controller action. After that it no longer exists. – Darin Dimitrov Aug 14 '12 at 21:16
  • what should be done in this case.I am playing with a lot of partial views and i do require to store the viewbag value,which i should be able to access from a view and a couple of partial views.That is my requirement.Any suggestions? – Soni Aug 14 '12 at 21:18