1

I need to pass a variable from C# to javascript in the form { 'key':'value', ..., }. I tried passing it as a string and hoping javascript would parse it (because the C# on cshtml pages is evaluated server side and js is client side) but unfortunately the quotes were formatted as &whateverthecodeis; so it didn't work. I think JSON might be what I'm looking for, but I have no idea how to use it.

tereško
  • 58,060
  • 25
  • 98
  • 150
davidjosepha
  • 117
  • 3
  • 14
  • You could still use JSON, you would have to remove the format of the string brought back. Dig a little deeper in here and you'll see how you can accomplish this. – Jose Aug 16 '13 at 19:02
  • Have you tried google'ing "c# json key value?" Here is one of the results that's pretty straightforward. Let me know if you have any questions http://stackoverflow.com/questions/4861138/c-sharp-json-serialization-of-dictionary-into-keyvalue-instead-of-keyk – user1477388 Aug 16 '13 at 19:04
  • If your application is an ASP.Net page then you can use RegisterClientScript/RegisterStartupScript to write your javascript to the page. It will be written to the page as it is without getting encoded. – Himanshu Kumar Aug 16 '13 at 19:11
  • @user1477388 Thanks very much! I had tried Googling, but not quite the right thing, apparently. I now have a JsonResult. Is there a way to pass this to my javascript without querying a page that returns the JsonResult? `@ViewData["CustomersSearch"]` is rendered as `System.Web.Mvc.JsonResult` – davidjosepha Aug 16 '13 at 19:26
  • @user1579458 if you want to pass data from c# to a javascript, you're talking about AJAX. You can accomplish this easiest via jQuery as shown here http://api.jquery.com/jQuery.ajax/ Here's a practical example for MVC http://stackoverflow.com/questions/16186083/making-a-simple-ajax-call-to-controller-in-asp-net-mvc – user1477388 Aug 16 '13 at 19:31

2 Answers2

3

Here is what I might do...

Run this console app and see what it does:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// note: you will have to include a reference to "System.Web.Extensions" in your project to be able to use this...
using System.Web.Script.Serialization;

namespace KeyValuePairTestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<KeyValuePair<string, string>> pairs = new List<KeyValuePair<string, string>>()
            {
                new KeyValuePair<string, string>("MyFirstKey", "MyFirstValue"),
                new KeyValuePair<string, string>("MySecondKey", "MySecondValue")
            };

            string json = new JavaScriptSerializer().Serialize(pairs);

            Console.WriteLine(json);
        }
    }
}

For the "pass to javascript" part, please see here Making a Simple Ajax call to controller in asp.net mvc for practical examples for MVC and jQuery.

Community
  • 1
  • 1
user1477388
  • 20,790
  • 32
  • 144
  • 264
1

Yes you can use JSON. Perhaps you should try using escape characters to escape the quotes being misinterpreted. Or as in the above answer @user1477388, serialize the keyvalupairs to Json and return as following:

  public ActionResult ReturnJsonObject()
 {
    //Your code goes here
    return Json(json);
}
sandeepani
  • 353
  • 3
  • 12