Category Archives: Principle

MVC2 Bug on Data Binding in Listbox?

Seems MVC and MVC 2 don’t realize that there is an issue when binding the data on the view:

Given an extension on HtmlHelper: this extension will generate a list box in view based on the collection “cakes”. (Because I like cake, so I use dessert object as example :)

public static MvcHtmlString MyDropDownList(this HtmlHelper target, string controlName, IQueryable<Cake> cakes, object htmlAttr)
{
if (cakes== null)
return MvcHtmlString.Empty;
var cakesList = new SelectList(cakes, “Id”, “CakesName”);
return target.ListBox(controlName, cakesList, htmlAttr);
}

It is okay, but the main issue is if the when I specify the Model data on control name, it IS SUPPOSED to generate the list box with the data which retrieved from database are selected. Yes.  IT IS SUPPOSED TO BE. But, unfortunately, MVC fail to automatically bind data for us…  The data “cakesList” is showing properly, but with no preselect options. In most of the business logic, it’s not gonna work, because it is no point for user to re-select the options every time before save the data.

In this case, MVC is not gonna auto-bind the data for us, we just need to figure out another way to display and manipulate the data properly. So we will change something on the extension, view and the controller:

1. On the view, don’t specify the name to model’s name, For example, the data which will be weaved to list box is CakeViewData.CakeList, the data related to database is CakeViewData.Dessert.Cakes. Normally, we will use the extension to weave the data:

<%=Html.MyDropDownList(“Dessert.Cakes”, Model.CakeList, new { @class = “multiselect”, size = 5 })%>

Since MVC is not gonna take care of the data binding, The name (on parameter controlName) should and NEED to use other name instead. Change “Cakes” to any other name, for example, change to “MyCakes”:

<%=Html.MyDropDownList(“Dessert.MyCakes”, Model.CakeList, new { @class = “multiselect”, size = 5 })%>

2. Extension is changed to:

public static MvcHtmlString MyListBoxWithSelectedList(this HtmlHelper target, string name, IQueryable<Cake> cakes, List<string> selected, object htmlAttr)
{
if (interests == null)
return MvcHtmlString.Empty;
var cakeList = new MultiSelectList(cakes, “Id”, “CakeName”, selected);
return target.ListBox(name, cakeList, htmlAttr);
}

I just add a parameter “List<string> selected” in the extension which will be used in MultiSelectList object that can be bind to the the items to be selected.

3. In controller, before saving the data, need to use FormCollection or Request["Dessert.MyCakes"] to obtain the data “MyCakes” manually.

string myCakes = Request["Dessert.MyCakes"];
Dessert dessert = new Dessert{Cake = myCakes};

It might not be a bug actually. And in this example you can feel how flexibility that MVC can bring you: the way to manipulate the model or data varies!  :)

Happy programming in MVC 2!

UP – the adventure of a lifetime

All his life is dreaming of adventure, finally, he is taking off!

It is a moving story at the very beginning when he is living with his wife before the exploration. It’s pity that he can’t take his wife who encourage him to take the adventure :(

What’s the story of the movie? Well, I think pixar is gonna tell us, an adventure could be a lifetime, as long as you still dream of it. But it would become a true story when stop dreaming, just take off today!

iKcod – Homebrew iPod/iPhone Car Dock

How to make it? Well, if you don’t like paper work nor D.I.Y, then skip it, go to $2 shop straight away, get yourself a $3 phone dock and hang it in your car. If you like D.I.Y, just make sure you have:

1. 210gsm paper from Office Work, about $1.50, Easy.

2. You might need a scissors and some staples.

3. Glue is optional instead of using staples.

Then cut the paper alongside the line, as the picture shown below.

ikcod_2

Fold it.

Hang it in your car.

That’s the pager dock for iPod or iPhone. Using with FM transmitter would be absolutely great! I call it “iKcod”, which is reversed of “Docki”, and match the pattern of “iPod”, it IS really nice.

ikcod

Got one logo from Rae, who is the game master of MapleStory as well as guitar master.

Subscribe Rae’s guitar stories here: http://www.youtube.com/user/theperformerz360
I was surprise when saw his youtube at the first time, I think will surprise everyone as well.

I got a logo from him. Subscribe Rae’s computer tips stories here: http://www.youtube.com/user/Ultraloadz
There you can see many tutorials on using softwares. Amazing little guy~

Ubuntu 9.0.4 kills me, or, I kill it?

I was too excited to have a newly released ubuntu system installed on my machine 2 days ago. Seems there are too many defects since it’s too fresh.. I suffered from xp-blue-screen-like system crash several times..

The system does not change too much from 8.10 as I imagine. :-( In my point of view, it’s becoming more and more fragile since version 7.

Should I go on with it or kill it?

– OK. He wins, I’ll keep it.