Yehia A.Salam
Yehia A.Salam

Reputation: 2078

ViewBag Assignment

I'm trying to pass a value from my database to the view in asp.net mvc, very simple stuff

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

However I keep getting this error

Cannot apply indexing with [] to an expression of type 'System.Dynamic.DynamicObject' 

Upvotes: 3

Views: 5580

Answers (1)

Paolo del Mundo
Paolo del Mundo

Reputation: 2121

It should be this instead:

ViewBag.app_id = app_id;

ViewBag is a dynamic type, meaning you can add parameters, like app_id, at run-time.

Upvotes: 12

Related Questions