Friday, March 22, 2013

Bootstrap collapse accordion not working on IE

I was struggling with this for sometime. Bootstrap's collapse accordion did not work on IE (7,8,9) but worked fine on Chrome. I am using Bootstrap version 2.3. People reported this issue for version 2.1 and there was a fix in the later version of Bootstrap. So, with v2.3 it should all be working. I tried a few things including pasting the code on jsfiddle and running it. It worked there. 
Finally I saw in my layout.cshtml (master page, if you may) that bootstrap.js and bootstrap.min.js, both have been included. I removed the .min script and boom, it started working. Why, what's up with bootstrap.min.js? I don't know. 


Thursday, March 14, 2013

OUTER APPLY with Linq

OUTER APPLY in Sql Server is a very convenient joining technique to achieve Left outer join with a group by. See the below example. 

Example: 

select Name, ea.eeacount, es.name 
From dbo.EnrollmentEntity ee 
OUTER APPLY
(
    Select count(eea.Id) as eeacount 
    From dbo.EnrollmentEntityAssister eea 
    Where eea.EnrollmentEntityId = ee.Id and eea.active =1 and ee.active = 1
)ea
OUTER APPLY
(
    Select TOP 1 ls.name 
    From dbo.EnrollmentEntityStatus ees 
    join dbo.LookupEnrollmentEntityStatus ls on ees.StatusId = ls.Id and ls.active = 1
    where ees.EnrollmentEntityId = ee.Id and ees.active = 1 and ee.active = 1
    Order by ees.UpdatedDate desc 
)es

This query gets all the EnrollmentEntity records with a count of the assisters for each Enrollment Entity and the latest status

To achieve similar in Linq, we can do something like this: 

var results = from enrollmentEntity in enrollmentEntities
        join assister in assisters on enrollmentEntity.Id equals assister.EnrollmentEntityId into eAssisters 
        join status in statuses on enrollmentEntity.Id equals status.EnrollmentEntityId into eStatuses 
        where enrollmentEntity.Active
         select new EnrollmentEntityGridViewModel
        {
            Id = enrollmentEntity.Id,
            Name = enrollmentEntity.Name,
            Assisters = eAssisters.Count(), 
            RenewalDate = "",
            Status =  eStatuses.OrderByDescending(p => p.UpdatedDate).FirstOrDefault().StatusIdSource.Name 
       };

Neat, huh!

Saturday, March 9, 2013

NEVER use HTML Helpers for Hidden fields (Asp.Net MVC)

 This is the second time around when I ran into this problem - Hidden fields on a view after rendering it back from the controller after POST, does not update the hidden fields with latest values. The reason, I found online is that the view rendering engine (Razor, in this case) first looks at the values being posted for the Helper fields and uses them rather than the ones, that were returned from the controller after the POST. What is the reason behind this design - I dont know. 

Example:
So, if you have a view like this - 


<div class="span9">
        @using (Html.BeginForm("Payment""Entity"FormMethod.Post))
        { 
            @Html.ValidationSummary("Please provide the required fields")
            @Html.HiddenFor(m => m.EntityId)
            .....


Instead do: 

 <div class="span9">
        @using (Html.BeginForm("Payment""Entity"FormMethod.Post))
        { 
            @Html.ValidationSummary("Please provide the required fields")
            <input type="hidden" name="EntityId" id="EntityId" value="@Model.EntityId" />


Hope you save some time. 

Friday, March 1, 2013

Bootstrap accordion collapse to show up collapsed by default

If you are like me, trying to code looking at the view-source or through InspectElement (on chrome) to implement the collapse example on Twitter's bootstrap page - http://twitter.github.com/bootstrap/javascript.html#collapse , you will end up having it not collapse all the div's by default on the page load. 

The culprit is "style="height:auto;" 

Remove the style and all the div's should be collapsed by default, on page load. 

Happy bootstrapping!