bill
bill

Reputation: 864

How to retrieve data from view bag in jquery(inside the view) in c#, mvc

I'm sending a List using ViewBag to the view like this.

List<RegionSearch> rs = searchregionArray.Select(sr => new RegionSearch()
{
    region = (string)sr["AREA/DEST"],

}).ToList();

var filteredRegion = rs.Select(k => k.region).Distinct();

//testing 
ViewBag.checking = rs;

now I want to get it in <script></script> .how can I do that

Upvotes: 0

Views: 655

Answers (1)

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18893

Try this :-

<script>
   var jsonList = '@Html.Raw(Json.Convert(ViewBag.checking))'
   var jsList = JSON.parse(jsonList);
   console.log(jsList);
</script>

NOTE :- Although not a good idea sending whole list from controller to view inside ViewBag instead use ViewModel.

Upvotes: 4

Related Questions