Passing list from MVC ViewBag to JavaScript

前端 未结 3 1472
执念已碎
执念已碎 2021-02-02 16:57

I have a list of users which I pass from my controller to my view using the view bag. Now I need to be able to pass the same list to the javascript on the page. I could reconstr

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-02 17:45

    Another option, could be to create a new action in your controller returning a JsonResult. This json result could return your list. In your page you can call the action with jquery and use it from there.

    public ActionResult GetMyList()
    {
        var list = GetMyUserList();
    
        return Json(new { userlist = list }, JsonRequestBehaviour.AllowGet);
    }
    

提交回复
热议问题