asp.net mvc - How to send a value from a select statement in MVC controller to Viewbag -


i want return value viewbag linq query below

var mycompanyname = (from c in db.companies                      c.companyid == companyid                      select c.companyname).tostring(); viewbag.mycompanyname = mycompanyname; 

in mvc controller following output in view.

select [extent1].[companyname] [companyname] [dbo].[companies] [extent1] [extent1].[companyid] = @p__linq__0 company name controller action  

the result viewbag expected view bag should like: companyname xyz. please how make right?

your not materializing query, , suspect want return single value (not ienumerable<string>) need replace .tostring() .firstordefault()

var mycompanyname = (from c in db.companies                      c.companyid == companyid                      select c.companyname).firstordefault(); viewbag.mycompanyname = mycompanyname; 

or if did want collection of string, .tolist()


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -