admin管理员组文章数量:1402857
In My ASPMVC Code I filled the ViewBag.Contacts variable by use the below code
var contactsGroups = EntityContext.DBContext.TS_GetConfirmedContacts(typeId,AccountId).GroupBy(c=>c.GroupName);
// JSONGroup Custom object
List<JSONGroup> jsonContactsGroups = new List<JSONGroup>();
foreach (var group in contactsGroups)
{
jsonContactsGroups.Add(new JSONGroup
{
GroupName = group.Key,
Objects = new List<object>(group.ToList())
});
}
var result = new JsonResult
{
Data = jsonContactsGroups,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
ViewBag.Contacts = result;
When I am tried to access the ViewBag.Contacts variable from javascript by this Code
var contactData = '@ViewBag.Contacts';
After debugging the value returned as string [from console]
var contactData = 'System.Web.Mvc.JsonResult';
In My ASPMVC Code I filled the ViewBag.Contacts variable by use the below code
var contactsGroups = EntityContext.DBContext.TS_GetConfirmedContacts(typeId,AccountId).GroupBy(c=>c.GroupName);
// JSONGroup Custom object
List<JSONGroup> jsonContactsGroups = new List<JSONGroup>();
foreach (var group in contactsGroups)
{
jsonContactsGroups.Add(new JSONGroup
{
GroupName = group.Key,
Objects = new List<object>(group.ToList())
});
}
var result = new JsonResult
{
Data = jsonContactsGroups,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
ViewBag.Contacts = result;
When I am tried to access the ViewBag.Contacts variable from javascript by this Code
var contactData = '@ViewBag.Contacts';
After debugging the value returned as string [from console]
Share Improve this question edited May 27, 2015 at 17:18 Osama AbuSitta asked May 24, 2015 at 10:29 Osama AbuSittaOsama AbuSitta 4,0664 gold badges37 silver badges54 bronze badgesvar contactData = 'System.Web.Mvc.JsonResult';
3 Answers
Reset to default 4You can not use JsonResult to return a Json formated string. JsonResult class is inherited from ActionResult class, so the returned value is an object containing Json formated 'Data' property and some other properties for response. so and using @ symbol to output the result , will return the name of class as string. if you want to serialize an object to a Json object , use Newtonsoft's Json.NET serializer or use the .NET build in JavaScriptSerializer class.
Newtonsoft :
String serializedResult = JsonConvert.SerializeObject(jsonContactsGroups);
JavaScriptSerializer :
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer() ;
String serializedResult = serializer.Serialize(result);
Then return the Json serialized string to the view :
ViewBag.Contacts = serializedResult;
But if you want to return a Json model or result to the view , i suggest you to write your action method as JsonResult method.
Use Html.Raw and Json.Encode like
var model = @Html.Raw(Json.Encode(ViewBag.Contacts));
// fetch data
$.each(model.Data, function() {
console.log($(this).attr("GroupName"));
});
Use JSON.Net(it may already be referenced since MVC uses it as default serializer)
using Newtonsoft.Json;
ViewBag.Contacts = JsonConvert.SerializeObject(jsonContactsGroups, Formatting.Indented);
本文标签: cCan39t access to ViewBag (have a list of JSON objects ) from javascriptStack Overflow
版权声明:本文标题:c# - Can't access to ViewBag (have a list of JSON objects ) from javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744368777a2602916.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论