Using ASP.Net MVC3 with C#.
Currently I am trying to pass a single database record to my view page creating my own ad rotator.
Here is my controller code.
var rand = new Random((int) DateTime.Now.Ticks);
int numIterations = 0;
numIterations = rand.Next(1, 2);
CJAd cjad = db.CJAds.Single(c => c.category_id == 1 && c.ad_active == true && c.id == numIterations);
ViewData["SideBarAd"] = cjad;
Every code sample I have seen shows how to cycle through multiple records. How do I turn the ViewData on the view page into a database object and display it?
It tried this
@ViewData["SideBarAd"].ad_url
But received the error
CS1061: ‘object’ does not contain a definition for ‘ad_url’ and no extension method ‘ad_url’ accepting a first argument of type ‘object’ could be found (are you missing a using directive or an assembly reference?)
Why don’t you just include the CJAd in your view model? That’s much simpler.
However, if you’re determined, use ViewBag instead, then you don’t need to cast.
in your view (assuming SideBarAd is a string that is the url to your ad)