Note that there are some explanatory texts on larger screens.

plurals
  1. PORazor helper method not working after upgrade from MVC 3 to 4
    text
    copied!<p>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...</p> <p>[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.</p> <p>[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. </p> <pre><code>@helper RenderConnectButton(System.Web.Mvc.HtmlHelper helper, ContactTwitterHandleDto contactTwitterHandle, ContactFacebookAccountDto contactFacebookAccount) { &lt;ul class="socialMedia inlineList"&gt; @if (contactTwitterHandle != null &amp;&amp; contactTwitterHandle.IsValid.HasValue &amp;&amp; contactTwitterHandle.IsValid.Value) { var tHandle = "@" + contactTwitterHandle.Handle; &lt;li class="twitter"&gt; &lt;a class="btn" href="http://www.twitter.com/@contactTwitterHandle.Handle" target="_blank"&gt; &lt;i&gt;&lt;/i&gt; &lt;span class="label"&gt;@tHandle&lt;/span&gt; &lt;/a&gt; @if(contactTwitterHandle.FollowerCount.HasValue) { &lt;div class="count" id="c"&gt; &lt;i&gt;&lt;/i&gt; &lt;u&gt;&lt;/u&gt; &lt;span class="followers" title="@contactTwitterHandle.FollowerCount.Value followers"&gt;@FollowerCount(contactTwitterHandle.FollowerCount.Value)&lt;/span&gt; &lt;/div&gt; } &lt;/li&gt; &lt;script type="text/javascript"&gt; $("#twitter").click(function () { window.location.href = '@(helper.BuildUrlFromExpression&lt;TenantController&gt;(t =&gt; t.LinkTwitterAccount()))'; }) &lt;/script&gt; } @if (contactFacebookAccount != null &amp;&amp; contactFacebookAccount.IsValid.HasValue &amp;&amp; contactFacebookAccount.IsValid.Value) { &lt;li class="facebook"&gt; &lt;a class="btn" href="@contactFacebookAccount.Url" target="_blank"&gt; &lt;i&gt;&lt;/i&gt; &lt;span class="label"&gt;@contactFacebookAccount.Name&lt;/span&gt; &lt;/a&gt; @if(contactFacebookAccount.FriendCount.HasValue) { &lt;div class="count" id="c"&gt; &lt;i&gt;&lt;/i&gt; &lt;u&gt;&lt;/u&gt; &lt;span class="followers" title="@contactFacebookAccount.FriendCount.Value friends"&gt;@FriendCount(contactFacebookAccount.FriendCount.Value)&lt;/span&gt; &lt;/div&gt; } &lt;/li&gt; } &lt;/ul&gt; } </code></pre> <p>[UPDATE] I changed the code a bit and now it's even stranger. I'm getting the same error as above at the line: </p> <pre><code>var tLink = "http://www.twitter.com/" + contactTwitterHandle.Handle; </code></pre> <p>contactTwitterHandle and contactTwitterHandle.Handle are both NOT null.</p> <pre><code>@helper RenderConnectButton(System.Web.Mvc.HtmlHelper helper, ContactTwitterHandleDto contactTwitterHandle, ContactFacebookAccountDto contactFacebookAccount) { &lt;ul class="socialMedia inlineList"&gt; @if (contactTwitterHandle != null &amp;&amp; contactTwitterHandle.IsValid.HasValue &amp;&amp; contactTwitterHandle.IsValid.Value) { var tHandle = "@" + contactTwitterHandle.Handle; var tLink = "http://www.twitter.com/" + contactTwitterHandle.Handle; &lt;li class="twitter"&gt; &lt;a class="btn" href="@tLink" target="_blank"&gt; &lt;i&gt;&lt;/i&gt; &lt;span class="label"&gt;@tHandle&lt;/span&gt; &lt;/a&gt; @if(contactTwitterHandle.FollowerCount.HasValue) { &lt;div class="count" id="c"&gt; &lt;i&gt;&lt;/i&gt; &lt;u&gt;&lt;/u&gt; &lt;span class="followers" title="@contactTwitterHandle.FollowerCount.Value followers"&gt;@FollowerCount(contactTwitterHandle.FollowerCount.Value)&lt;/span&gt; &lt;/div&gt; } &lt;/li&gt; &lt;script type="text/javascript"&gt; $("#twitter").click(function () { window.location.href = '@(helper.BuildUrlFromExpression&lt;TenantController&gt;(t =&gt; t.LinkTwitterAccount()))'; }) &lt;/script&gt; } @if (contactFacebookAccount != null &amp;&amp; contactFacebookAccount.IsValid.HasValue &amp;&amp; contactFacebookAccount.IsValid.Value) { &lt;li class="facebook"&gt; &lt;a class="btn" href="@contactFacebookAccount.Url" target="_blank"&gt; &lt;i&gt;&lt;/i&gt; &lt;span class="label"&gt;@contactFacebookAccount.Name&lt;/span&gt; &lt;/a&gt; @if(contactFacebookAccount.FriendCount.HasValue) { &lt;div class="count" id="c"&gt; &lt;i&gt;&lt;/i&gt; &lt;u&gt;&lt;/u&gt; &lt;span class="followers" title="@contactFacebookAccount.FriendCount.Value friends"&gt;@FriendCount(contactFacebookAccount.FriendCount.Value)&lt;/span&gt; &lt;/div&gt; } &lt;/li&gt; } &lt;/ul&gt; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload