Wednesday, February 9, 2011

ASP.NET MVC3 Razor Notes - Partial Views

What's the difference between Html.RenderPartial() and Html.Partial()?

Html.RenderPartial() is an HTML helper method that was introduced in MVC1 and writes directly into the response object.

Html.Partial() is an HTML helper method that was introduced in MVC2 and like the other HTML helper methods returns a string and does not write into the response object.

Both helpers are used with partial views (sometimes still called controls) to implement the DRY principal of shared View code across multiple pages.

In the MVC WebForms View Engine views have the extension .aspx and partial views .ascx. This makes it easy to distinguish between views and partial views. In the MVC Razor View Engine both views and partial views have the extension of .cshtml. In order to easily distinguish between a view and a partial view in your project I suggest that you precede the partial views name with an underscore.

ASP.NET MVC3 Razor Notes - view data

The WebViewPage in an abstract class that inherits from the WebViewPage<TModel> generic abstract class.

Two properties of the WebViewPage<TModel> generic abstract class are:

  1. Model (dynamic)
  2. ViewData (key/value collection)

Among other properties, the WebViewPage adds:

  1. ViewBag (dynamic)

The ViewBag property is another way to get to the ViewData data.

For example:

If in the controller you did the following:

ViewData["website"] = "guyellisrocks.com";
ViewData["somenumbers"] = Enumerable.Range(0, 10);

Then in the view you could access that data through the ViewBag:

<div>
    @ViewBag.website
</div>
@foreach (int i in ViewBag.somenumbers)
{
<div>
    @i
</div>
}

and this would emit to the web page:

guyellisrocks.com
0
1
2
3
4
5
6
7
8
9
 

ASP.NET MVC3 Razor Notes - switching between text and code

Switching between text and code in Razor:

If you're in a code block and want to emit text you can use the <text> tag:

@{
   if(condition == true) {
      <text>this is true text</text>
   } else {
      <text>this is false text</text>
   }
}

The <text> tag will not be included in the generated HTML.

If you're in a text section and want to explicitly invoke code that could be confused as text you can wrap the code in parenthesis:

<img src="@(filename).jpg" />

If you had not put the () parens in above it would have tried to evaluate the .jpg as a property on the filename object.

 

SEO Top Negative Ranking Factors

Great search engine ranking factors article recently published SEOmoz.

Point #2 in the Top 5 Negative Ranking Factors is "Link Acquisition from Known Link Brokers/Sellers." I still find this item hard to believe although I've heard this type of statement before.

I remember attending an SEO talk where an "expert" said that links from "bad" sites to your site could harm your SEO. I asked the presenter if she was able to provide any evidence of this and she said that she could not. To date, I have not seen any evidence of this, and as I said I find it hard to believe and this is why:

A competitor could publish links to your site from "bad" sites and/or buy links to your site on a link broker's site to degrade your SEO performance and allow him/her to rise above you in search results. Are you telling me that the engineers that work on the search engine algorithms have not thought of that? These are clever guys, believe me, they've thought of this.

In my opinion, link building from "bad" sites or buying from link brokers will not have a negative impact on your SEO, it will only have a negative impact on your time and a neutral impact on your SEO. i.e. you're wasting your time by doing it as it will have no impact.

Monday, February 7, 2011

ASP.NET MVC3 does not appear as an option in VS2010 after installing it

I couldn't work out why the option to create an "ASP.NET MVC 3 Web Application" was not appearing in the list of templates when I clicked on File > New > Project... in Visual Studio 2010. Only "ASP.NET MVC 2 Web Application" was listed there.

Turns out there's a drop down to allow you to select your framework at the top of the file/new/project dialog and this was set to the .NET Framework 3.5. Because MVC3 needs .NET 4 it wasn't being listed. Changing the framework to ".NET Framework 4" solved the problem.

After doing that VS2010 will remember your last choice and default to ".NET Framework 4"...

Named parameters in a URL

Bill Brown was showing me his NLP links in Pinboard when I noticed that he was typing named parameters in the pinboard URL. I'm intimately familiar with named parameters which have been around for a long time in computer languages and have recently been introduced into C# and am a big fan of them. However, I have never seen them used (or thought of using them) in a URL before, here is an example:

http://pinboard.in/u:bbrown/t:python/t:nlp

In this URL we're saying find user (key=u, value=bbrown) named bbrown and all items that he's tagged (key=t) with both python and nlp. If we swapped the tags around the query produces the same results:

http://pinboard.in/u:bbrown/t:nlp/t:python

All is not perfect, however, in Utopia. If you swap the user param to after the tag param it won't work. This link takes you to a broken page:

http://pinboard.in/t:nlp/t:python/u:bbrown

This is fairly easy to fix and I'm guessing that the guys at pinboard.in don't think that many people will be hacking their url's and so didn't put the time into making them work in any order like true named parameters would.

It's still great to such innovation in the use of the url without using the query param which would have achieved the same result. Out of curiosity I tried:

http://pinboard.in/?u=bbrown&t=nlp

but that doesn't translate for them.