Archive for March, 2013

Populating a Select list in MVC 4 from code-behind

I wanted to populate a list of states on my home page in an MVC project. So I opened my HomeController.cs class and added this function:

        private static List<SelectListItem> PopulateStates()
        {
            List<string> states = new List<string>() { “AL”, “AK”, “AZ”, “AR”, “CA”, “CO”, “CT”, “DE”, “DC”, “FL”, “GA”, “HI”, “ID”, “IL”, “IN”,
                “IA”, “KS”, “KY”, “LA”, “ME”, “MD”, “MA”, “MI”, “MN”, “MS”, “MO”, “MT”, “NE”, “NV”, “NH”, “NJ”, “NM”, “NY”, “NC”, “ND”, “OH”,
                “OK”, “OR”, “PA”, “RI”, “SC”, “SD”, “TN”, “TX”, “UT”, “VT”, “VA”, “WA”, “WV”, “WI”, “WY” };
            var final = from s in states
                        select new SelectListItem
                        {
                            Text = s
                        };
            return final.ToList();
        }

As you see, I’m populating a list of SelectListItem classes, and then converting them to a List<SelectListItem > .

Still in HomeController.cs, I modified the Index method to populate the ViewData object with the List returned from the method:

        public ActionResult Index()
        {
            ViewData[“States”] = PopulateStates();
            return View();
        }

And in my Index.cshtml method, I use that ViewData object to populate a DropDownList, which is done using  Html.DropDownList:

@Html.DropDownList(“ddlStates”,(IEnumerable<SelectListItem>)ViewData[“States”])

Leave a comment

Out of control cell height in Reporting Services

I was in working Reporting Services and one of my cell kept growing to be humongous. Only one cell, and only if I included the value from a certain table. So imagine you have this setup:

SELECT c.Name, o.OrderId, p.ProductName
FROM Customer c
INNER JOIN Order o
ON o.CustomerId = c.CustomerId
INNER JOIN Product p
ON p.OrderId = p.OrderId

Very advanced stuff here.

I had a basic Tablix, and when I included all the fields, the table containing ProductName grew to prodigious height. Pages were consumed by the evil cell as it tried to take over the world. I tried restricting the size, trimming the data in the report, and trimming the data in the query – nothing worked. But when I excluded that field from the query, everything looked fine.

The problem was my query. If you didn’t notice the error in the query, look at the last line:

ON p.OrderId = p.OrderId

Oops. Once I changed the query to what it should have been:

ON o.OrderId = p.OrderId

All was well – the cell no longer had a height of a scazillion. So if you have an out-of-control cell, make sure your query doesn’t suck like mine did.

Leave a comment