Access viewData in JquerySonu Chaudhary10yAsked Mar 15, 2016, 4:35 AM EDT8 answersShareLinkedInXCopy linkSave how to access ViewData data in jQuery in a variable.
Access viewData in JquerySonu Chaudhary10yAsked Mar 15, 2016, 4:35 AM EDT8 answersShareLinkedInXCopy linkSave how to access ViewData data in jQuery in a variable.
Saillesh PawarRank #537 · 4.5K reputation10y agoPosted Mar 15, 2016, 5:04 AM EDT0public ActionResult Index() { ViewData["Sample"] = "date"; return View(); } $(document).ready(function () { debugger; var success ='@((ViewData["Sample"]).ToString())'; alert(success); });0
Sonu ChaudharyRank #1,045 · 1.8K reputation10y agoPosted Mar 15, 2016, 11:25 PM EDT0Saillesh its not working on my view0
Saillesh PawarRank #537 · 4.5K reputation10y agoPosted Mar 16, 2016, 12:48 AM EDT0tell me the error you are facing0
Saillesh PawarRank #537 · 4.5K reputation10y agoPosted Mar 16, 2016, 12:55 AM EDT0TEST the sample code for your reference0
Amit KumarRank #459 · 5.4K reputation10y agoPosted Mar 16, 2016, 1:23 AM EDT0Hi dear, Please use it. First: var _myNames = JSON.parse('@Html.Raw(Json.Encode(ViewData["myNames"]))');//which will return something like suppose[{ID:1, Name:"FirstName"}, {ID:2, Name:"SecondName"}, ...]//You can then access it with$.each(_myNames, function(index, item) { var id = item.ID; var name = item.Name;}); Second : <% var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();%> var users = <%= serializer.Serialize(ViewData["UserList"]) %>; //Use the users variable now with a copy of the view data. OR@foreach (var myName in (List)ViewData["myNames"]){ FormatName('@myName.Name', 1); } 0Sonu ChaudharyRank #1,045 · 1.8K reputation10y agoPosted Mar 16, 2016, 2:24 AM EDT0 thanks amit serializer work for me.....0Amit KumarRank #459 · 5.4K reputation10y agoPosted Mar 16, 2016, 3:34 AM EDT0Ok JI, Your problem is resolve or not. 0Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign inKnow the answer?Answer a question in the forums and earn +20 SHARP for every answer.See unanswered questionsForumsTop experts01Arun PottiAnswers in the forums20answers02Tahir AnsariAnswers in the forums5answers03Kamlesh KumarAnswers in the forums5answers04Sam HobbsAnswers in the forums4answers05Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.JoinFull leaderboard →2026DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sonu ChaudharyRank #1,045 · 1.8K reputation10y agoPosted Mar 16, 2016, 2:24 AM EDT0 thanks amit serializer work for me.....0
Amit KumarRank #459 · 5.4K reputation10y agoPosted Mar 16, 2016, 3:34 AM EDT0Ok JI, Your problem is resolve or not. 0
Saillesh PawarRank #537 · 4.5K reputationPosted Mar 15, 2016, 5:04 AM EDT
Sonu ChaudharyRank #1,045 · 1.8K reputationPosted Mar 15, 2016, 11:25 PM EDT
Saillesh PawarRank #537 · 4.5K reputationPosted Mar 16, 2016, 12:46 AM EDT
Saillesh PawarRank #537 · 4.5K reputationPosted Mar 16, 2016, 12:48 AM EDT
Saillesh PawarRank #537 · 4.5K reputationPosted Mar 16, 2016, 12:55 AM EDT
Amit KumarRank #459 · 5.4K reputationPosted Mar 16, 2016, 1:23 AM EDT
Sonu ChaudharyRank #1,045 · 1.8K reputationPosted Mar 16, 2016, 2:24 AM EDT
Amit KumarRank #459 · 5.4K reputationPosted Mar 16, 2016, 3:34 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts