asp.net-mvc - ViewBag 分配

标签 asp.net-mvc asp.net-mvc-3 viewbag

我试图将一个值从我的数据库传递到 asp.net mvc 中的 View ,非常简单的东西

    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();
    }

但是我不断收到此错误
Cannot apply indexing with [] to an expression of type 'System.Dynamic.DynamicObject' 

最佳答案

应该是这样的:

ViewBag.app_id = app_id;

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

关于asp.net-mvc - ViewBag 分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11250661/

相关文章:

c# - 与 Web API 共享的 MVC 身份验证

asp.net-mvc-3 - MVC3 中的模型级错误?

asp.net-mvc-3 - 发布 mvc 3 应用程序时未创建某些文件夹

asp.net-mvc-4 - MVC4 ViewBag 还是 ViewModel 还是?

c# - MVC : Displaying Roles in a list of checkboxes, 然后保存它们

javascript - 如何将列表从 C# Angular 传递到 Angular Controller ?

asp.net-mvc-3 - MVC 3 脚手架可以在 n 层应用程序中使用吗?

asp.net-mvc - MVC - 将变量存储到下一个请求

c# - 清除 ViewBag?

asp.net-mvc - ASP.NET MVC。通过 viewData 传递列表