My Software Notes

Useful things I discover

Archive for February 2010

ASP.NET MVC SelectList Constructor

with 12 comments

I was creating a drop down list in an MVC application and the list was okay, but the selected item was not being selected.

I was using the SelectList object and the constructor that takes four parameters, the last parameter being selectedValue.  The documentation on MSDN is not exactly enlightening.  All it says for the parameter is “The selected value”.  Wow, big help.

This is what I finally figured out.

Here is the constructor:

public SelectList(
    IEnumerable items,
    string dataValueField,
    string dataTextField,
    Object selectedValue
  • items is the list of “things” you want to put in the drop down list (“select” HTML tag).
  • dataValueField is the name of the property on your “things” that you want to be put into the “value” attribute of the “option” tag rendered in the HTML.
  • dataTextField is the name of the property on your “things” that you want to be displayed in the drop down list (the text between the “<option>” and “</option>” in the “select”) .
  • selectedValue is the value you want to end up as selected in the drop down list. It has to be the same as what will be put into the “value” attribute of the “option” tag in the rendered HTML.

Example:  I have a class called Thing that has a property “ID” and a property “Description”.  I want something like this:

<option value="1">Description One</option>
<option value="2" selected="selected">Description Two</option>
<option value="3">Description Three</option

I have a List<Thing> that I will use to populate the drop down. I have a Thing that is the selected Thing.

List<Thing> things = GetThings();
Thing selectedThing = GetSelectedThing();
var selectList = new SelectList(things, "ID", "Description", selectedThing.ID);
ViewData["things"] = selectList;

So it works if the fourth constructor parameter is the exact value you want to match.  I’m not sure if it needs to be a string or not, I’ve not had time to check that.

03/11/2013 – I just came across these two excellent articles on using the Html.DropDownList and Html.DropDownListFor helper methods.  These might also shed some light on how to work with SelectList and why you may not even need to:

Drop-down Lists and ASP.NET MVC

DropDownListFor with ASP.NET MVC



Written by gsdwriter

February 17, 2010 at 4:56 pm

Posted in ASP.NET MVC

Changing your password while in a Remote Desktop session

with 31 comments

When your password is about to expire on a remote network which you access via remote desktop then how do you change your password?

Easy just press Ctrl-Alt-Del and the Windows Security screen (“Lock this computer/Switch User/ … /Change a password/…”) comes up.

Errr, well, actually, no it doesn’t.  The screen for your local computer comes up, not the screen on the remote computer.

So what now?  Do you just quietly sob?  Do you scream at the remote desktop machine and throw things at your monitor?

Never fear.  All is not lost.  Try “Ctrl-Alt-End” and, surprise, surprise, the screen you want on the remote machine appears and you can now change your pesky password.

For info on more keyboard shortcuts while in a Remote Desktop session:  Remote Desktop Tips and Tricks

Written by gsdwriter

February 9, 2010 at 10:01 am

Posted in Uncategorized