Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor helper method not working after upgrade from MVC 3 to 4

I have a helper method in a file under the App_Code directory. The method works perfectly fine in MVC 3, but after upgrading to MVC 4 it doesn't. It's failing at the first @if() block...

[Edit] This looks like a parsing error in the Razor engine. I'm getting the 'Object reference not set to an instance of an object' error when all my objects and their properties are NOT null. I guess I'll have to go to Microsoft directly.

[Edit] The error message is the old 'Object reference not set to an instance of an object.' This is weird because each object AND property in the condition are valid and not null.

@helper RenderConnectButton(System.Web.Mvc.HtmlHelper helper, ContactTwitterHandleDto contactTwitterHandle, ContactFacebookAccountDto contactFacebookAccount) {
    <ul class="socialMedia inlineList">           
        @if (contactTwitterHandle != null && contactTwitterHandle.IsValid.HasValue && contactTwitterHandle.IsValid.Value)
        {
            var tHandle = "@" + contactTwitterHandle.Handle;
            <li class="twitter">
                <a class="btn" href="http://www.twitter.com/@contactTwitterHandle.Handle" target="_blank">
                    <i></i>
                    <span class="label">@tHandle</span> 
                </a>
                @if(contactTwitterHandle.FollowerCount.HasValue)
                {
                    <div class="count" id="c">
                        <i></i>
                        <u></u>
                        <span class="followers" title="@contactTwitterHandle.FollowerCount.Value followers">@FollowerCount(contactTwitterHandle.FollowerCount.Value)</span>
                    </div>
                }                    
            </li>
            <script type="text/javascript">
                $("#twitter").click(function () {
                    window.location.href = '@(helper.BuildUrlFromExpression<TenantController>(t => t.LinkTwitterAccount()))';
                })
            </script>
        }

        @if (contactFacebookAccount != null && contactFacebookAccount.IsValid.HasValue && contactFacebookAccount.IsValid.Value)
        {
            <li class="facebook">
                <a class="btn" href="@contactFacebookAccount.Url" target="_blank">
                    <i></i>
                    <span class="label">@contactFacebookAccount.Name</span> 
                </a>
                @if(contactFacebookAccount.FriendCount.HasValue)
                {
                    <div class="count" id="c">
                        <i></i>
                        <u></u>
                        <span class="followers" title="@contactFacebookAccount.FriendCount.Value friends">@FriendCount(contactFacebookAccount.FriendCount.Value)</span>
                    </div>
                }
            </li>                
        }
    </ul> }

[UPDATE] I changed the code a bit and now it's even stranger. I'm getting the same error as above at the line:

var tLink = "http://www.twitter.com/" + contactTwitterHandle.Handle; 

contactTwitterHandle and contactTwitterHandle.Handle are both NOT null.

@helper RenderConnectButton(System.Web.Mvc.HtmlHelper helper, ContactTwitterHandleDto contactTwitterHandle, ContactFacebookAccountDto contactFacebookAccount) {
    <ul class="socialMedia inlineList">           
        @if (contactTwitterHandle != null && contactTwitterHandle.IsValid.HasValue && contactTwitterHandle.IsValid.Value)
        {
            var tHandle = "@" + contactTwitterHandle.Handle;
            var tLink = "http://www.twitter.com/" + contactTwitterHandle.Handle;
            <li class="twitter">
                <a class="btn" href="@tLink" target="_blank">
                    <i></i>
                    <span class="label">@tHandle</span> 
                </a>
                @if(contactTwitterHandle.FollowerCount.HasValue)
                {
                    <div class="count" id="c">
                        <i></i>
                        <u></u>
                        <span class="followers" title="@contactTwitterHandle.FollowerCount.Value followers">@FollowerCount(contactTwitterHandle.FollowerCount.Value)</span>
                    </div>
                }                    
            </li>
            <script type="text/javascript">
                $("#twitter").click(function () {
                    window.location.href = '@(helper.BuildUrlFromExpression<TenantController>(t => t.LinkTwitterAccount()))';
                })
            </script>
        }

        @if (contactFacebookAccount != null && contactFacebookAccount.IsValid.HasValue && contactFacebookAccount.IsValid.Value)
        {
            <li class="facebook">
                <a class="btn" href="@contactFacebookAccount.Url" target="_blank">
                    <i></i>
                    <span class="label">@contactFacebookAccount.Name</span> 
                </a>
                @if(contactFacebookAccount.FriendCount.HasValue)
                {
                    <div class="count" id="c">
                        <i></i>
                        <u></u>
                        <span class="followers" title="@contactFacebookAccount.FriendCount.Value friends">@FriendCount(contactFacebookAccount.FriendCount.Value)</span>
                    </div>
                }
            </li>                
        }
    </ul>
}
like image 333
Silkster Avatar asked May 16 '12 16:05

Silkster


1 Answers

I found the work-around for this problem, but first a little history. For this particular MVC project, we decided to start implementing Razor gradually. The app has a lot of "modal" areas which are filled by making an AJAX call to get a partial view. These partial views were the first to get implemented as Razor views. Later, we wanted to implement a new feature in a standard ASPX view page using a Razor partial view, but the ASPX view engine cannot render a partial Razor view so instead of using a Razor view I added a MVC helper file in the App_Code folder with a method that renders the feature. This method is what is causing the error after upgrading to MVC4. The page with the feature rendered by this method is what I'm converting to a Razor view page so I realized today that all I need to do is move the code from that method into a Razor partial view. This fixed the problem, however, I still maintain that this is a bug in the Razor engine for rendering with helper methods.

like image 68
Silkster Avatar answered Nov 15 '22 04:11

Silkster