ASP.NET MVC Html.RadioButtonList Blues

Recently I upgraded our ASP.NET MVC project from Preview 5 to RC2. At first I thought the Html.RadioButtonList extension was removed completely, but then realized that it was no longer in the main MVC assembly, but was moved to the Futures project (although I don't know why).

The Preview 5 version of the Html.RadioButtonList rendered the following output...

However, once I got my code to compile I went to run my application only to find that it rendered two RadioButtons with no labels! Where did the labels go?

I pulled down the source code for the 1.0 release (just to make sure it wasn't fixed in the RTM as opposed to the RC2) and dove into the extension code. Nowhere did I see the labels being applied in the extension.


//C:\...\MVC-RTM\MVC\src\MvcFutures\Mvc\RadioExtensions.cs
private static string[] RadioButtonListInternal(this HtmlHelper htmlHelper, string name, IEnumerable selectList, bool usedViewData, IDictionary<string, object> htmlAttributes) {
if (String.IsNullOrEmpty(name)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
}
if (selectList == null) {
throw new ArgumentNullException("selectList");
}

// If we haven't already used ViewData to get the entire list of items then we need to
// use the ViewData-supplied value before using the parameter-supplied value.
if (!usedViewData) {
object defaultValue = htmlHelper.ViewData.Eval(name);

if (defaultValue != null) {
IEnumerable defaultValues = new[] { defaultValue };
IEnumerable values = from object value in defaultValues select Convert.ToString(value, CultureInfo.CurrentCulture);
HashSet selectedValues = new HashSet(values, StringComparer.OrdinalIgnoreCase);
List newSelectList = new List();

foreach (SelectListItem item in selectList) {
item.Selected = (item.Value != null) ? selectedValues.Contains(item.Value) : selectedValues.Contains(item.Text);
newSelectList.Add(item);
}

selectList = newSelectList;
}
}

IEnumerable radioButtons = selectList.Select<SelectListItem, string>(item => htmlHelper.RadioButton(name, item.Value, item.Selected, htmlAttributes));

return radioButtons.ToArray();
}
</pre>

Next I looked at the Unit Tests for the extesion. I was please to see tests for the RadioButtonList, but was shocked to see that the asserts were verifying html that didn't have any labels applied!


//C:\...\MVC-RTM\MVC\test\MvcFuturesTest\Mvc\Test\RadioExtensionsTest.cs
[TestMethod]
public void RadioButtonListItemSelected() {
// Arrange
HtmlHelper htmlHelper = TestHelper.GetHtmlHelper(new ViewDataDictionary());

// Act
string[] html = htmlHelper.RadioButtonList("FooList", GetRadioButtonListData(true));

// Assert
Assert.AreEqual(@"<input id=""FooList"" name=""FooList"" type=""radio"" value=""foo"" />", html[0]);
Assert.AreEqual(@"<input id=""FooList"" name=""FooList"" type=""radio"" value=""bar"" />", html[1]);
Assert.AreEqual(@"<input checked=""checked"" id=""FooList"" name=""FooList"" type=""radio"" value=""baz"" />", html[2]);
}

private static SelectList GetRadioButtonListData(bool selectBaz) {
List list = new List();
list.Add(new RadioItem { Text = "text-foo", Value = "foo" });
list.Add(new RadioItem { Text = "text-bar", Value = "bar" });
list.Add(new RadioItem { Text = "text-baz", Value = "baz" });
return new SelectList(list, "value", "TEXT", selectBaz ? "baz" : "something-else");
}

private class RadioItem {
public string Text {
get;
set;
}

public string Value {
get;
set;
}
}
</pre>

Where does that leave us? I guess using the Futures Html.RadioButtonList isn't all that helpful after all. So, instead I decided to loop through my DataSource and create individual Html.RadioButton and Label combinations.



<%
var radioButtonList = new SelectList(new List {
new ListItem { Text = "Current", Value="false", Selected=true },
new ListItem { Text = "Other", Value="true"}}, "Value", "Text", "false");
var htmlAttributes = new Dictionary<string, object> {
{ "class", "radioButtonList" },
{ "onclick", "if(eval(this.value)) { $('#tblDate').show('slow'); } else { $('#tblDate').hide('slow'); }" }
};
foreach (var radiobutton in radioButtonList) { %>
<%=Html.RadioButton("rblDate", radiobutton.Value, radiobutton.Selected, htmlAttributes)%>

<% } %>
</pre>
Read More »

CodeStock 2009 Call for Speakers

<p>CodeStock 2009 is just around the corner.</p><p>CodeStock is a .NET developer’s conference held in Knoxville, TN on June 26-27, 2009.</p><p>This is a great opportunity to hear lots of great speakers and collaborate with other geeks for only $25!</p><p>If you are at all interested in speaking at CodeStock then I encourage you to submit your abstracts today!. You have until March 30th to submit yourself as a speaker.</p><p>Here is a snippet from their website…</p><blockquote><p>CodeStock 2009 is going to be held on June 26th and 27th. Registration will open on or after March 31st and the cost for CodeStock will stay the same as 2008 - $25.</p> <p>CodeStock will be held on June 26th and 27th, 2009 in Knoxville, TN. Speaker applications are due March 30th. Speaker selections will be announced on or before May 15th.</p></blockquote>

Read More »

ASP.NET MVC 1.0 RTM, Source, Docs, Videos, and More

For all of you who've been waiting long and hard, the time has finally come... ASP.NET MVC has been released!

You can download the 1.0 release from the Microsoft Downloads site.

You can also view the documentation for ASP.NET MVC on the MSDN Microsoft Developer Network.

Updated: If you are the curious type or just want to see how something works, then you can download the full source code (including all the tests and the futures project) from codeplex.

For many of you this means you finally might be able to convince your boss to use ASP.NET MVC in one of your production applications :)

Thanks to everyone involved in making ASP.NET MVC a reality!

If you would like a good start on learning the ASP.NET MVC, then I recommend checking out...

Read More »

Twitter Stats using Greasemonkey & jQuery

I wrote a simple Greasemonkey script that uses jQuery to read the Following, Followers, and Updates count from your Twitter Home page and displays them in the title of your Firefox tab (as seen in the image to the left).

Since I enjoy using jQuery, I tend to use this script as the starting point for several other of my custom Greasemoney scripts.

You can install the following Greasemonkey script from the userscripts.org website.

// ==UserScript==
// @name Twitter Stats
// @namespace http://zi.ma/webdev
// @description Display your Twitter Stats in the Tab Title
// @include http://twitter.com/home
// ==/UserScript==

//BEGIN - Load jQueryhttp://is.gd/j6G
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://jquery.com/src/jquery-latest.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);

function GM_wait() {
if (typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
else { $ = unsafeWindow.jQuery; letsJQuery(); }
}
GM_wait();
//END - Load jQuery http://is.gd/j6G

// All your GM code must be inside this function
function letsJQuery() {
unsafeWindow.console.log('BEGIN letsJQuery');

var followers = $("#follower_count").html();
var following = $("#following_count").html();
var updates = $("#update_count").html();
document.title = 'Twitter / Ing: ' + $.trim(following) + '; Ers: ' + $.trim(followers);

unsafeWindow.console.log('END letsJQuery');
}

unsafeWindow.alert = function alert(message) {
//do nothing
};

The above script first loads the jQuery framework and then retrieves the Twitter values.

You might notice the use of unsafeWindow.console.log('Your message here...');

I mainly used this to help debug the script.

You can find these messages displayed in your Firebug console.

Once you have installed the script, it could be easily be coupled with the ReloadEvery firefox extension so that you can sit back and watch your stats update to your heart's content :)

Read More »

Screencast: Dive Into ASP.NET MVC RC2


Dive into ASP.NET MVC RC2 from Elijah Manor on Vimeo.
<p>Last week I gave a presentation at the Compuware Thought Leadership meeting entitled, “Dive Into ASP.NET MVC RC2”.</p> <p>Several people asked me about the presentaiton and were interested in the material. However, the meeting was closed to only Compuware employees. So, I thought I would make a screencast of the talk.</p> <p>The screencast covers the following: </p><ol><li>An quick overview of what is “MVC”</li><li>Pros & Cons comparing ASP.NET MVC to ASP.NET WebForms</li> <li>Answers to questions you might be thinking</li><li>Review of new features since the beta release</li><li>Demonstration & code review of a demo app using ASP.NET MVC RC2</li></ol><p>Note: This is my first screencast, so please be patient with me :)</p> <p>You can download the sample ASP.NET MVC RC2 application demonstrated in the screencast… PetShop5Rc2.zip</p>

Read More »