我需要在js文件上获取资源数据。
所以我想通过ajax回调将资源数据从控制器动作传输到js文件。
怎么做?
我在asp.net mvc 5
答案 0 :(得分:1)
我是这样做的:
控制器动作:
[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) {
}
});
}