This project is read-only.

How to set a column title dynaically

Jul 10, 2013 at 1:15 PM
Hi,

How can I set the title of a column dynamically? I've set the text for the title in a viewbag object in the controller but trying to access the viewbag from within the grid call causes the following error:

"CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"

my code:
                    .Titled(ViewBag.PromoTitle1)
                    .Encoded(false)
                    .Sanitized(false)
                    .RenderValueAs(c => Html.CheckBox("cbIP1_"+c.ProductCode, c.isInPromotion1).ToHtmlString());
                    columns.Add(c => c.isInPromotion1)
the error occurs on the rendervalueas line...

Thanks for any help!

Cheers!
dj
Jul 10, 2013 at 3:08 PM
Hi,
ViewBag - it is a dynamic object, you need to case you property to string:
.Titled(ViewBag.PromoTitle1.ToString())
Jul 10, 2013 at 3:22 PM
Hi Bukharin,

Thanks for your reply, I tried that and I still get the same error, any further suggestions?

cheers!
dj
Jul 10, 2013 at 4:22 PM
Try to cast it explicitly:
.Titled((string)ViewBag.PromoTitle1)
Jul 10, 2013 at 4:46 PM
Hi Bukharin,

Thanks! that did it....

Cheers!
dj