ViewBag分配

Yeh*_*lam 3 asp.net-mvc viewbag asp.net-mvc-3

我正在尝试将数据库中的值传递给asp.net mvc中的视图,非常简单

    public ActionResult SettingsList() {


        using (podio_doc_db db = new podio_doc_db() ) {

            string app_id = db.Configs.Where(r => r.Key == "app.id").Select(r => r.Value).First();
            ViewBag["app_id"] = app_id;

        }

        return View();
    }
Run Code Online (Sandbox Code Playgroud)

但是我一直收到这个错误

Cannot apply indexing with [] to an expression of type 'System.Dynamic.DynamicObject' 
Run Code Online (Sandbox Code Playgroud)

Pao*_*ndo 12

它应该是这样的:

ViewBag.app_id = app_id;
Run Code Online (Sandbox Code Playgroud)

ViewBag是一种动态类型,这意味着您可以在运行时添加app_id等参数.

  • 非常尴尬:) (2认同)