user2522878
user2522878

Reputation: 39

Transfer resource file data from controller to js file

I need to get the resource data on js file. so I want to transfer the resource data from controler action to js file by ajax callback. How to do this?
I'm working in asp.net mvc 5

Upvotes: 0

Views: 318

Answers (1)

user2522878
user2522878

Reputation: 39

I'm do it's so:
controller Action:

 [HttpPost]
  [AllowAnonymous]
  [ValidateAntiForgeryToken]
public ActionResult GetCultureResource()
{
      ResourceSet resourceSet = 
  Resources.Resources.ResourceManager.GetResourceSet(new  System.Globalization.CultureInfo(cultureName), true, true);
   var dicResource= resourceSet.Cast<DictionaryEntry>()
                  .ToDictionary(x => x.Key.ToString(),
                                x => x.Value.ToString());

   var jsonString = JsonConvert.SerializeObject(dicResource);

  return Json(new { resource = jsonString});

 }

javascript fanction:

function SetCultureResource() {
    $.ajax({
        type: "POST",
        url: "/ControllerName/GetCultureResource",
        dataType: "json",
        success: function (data) {
            var obj = jQuery.parseJSON(data.resource);
        //do somthing as this with Resource
        //alert(Resource.BeforLogOut);
        },
        error: function (data) {

        }
    });
} 

Upvotes: 1

Related Questions