Using HtmlHelper.Select

Last post 03-17-2008 11:56 AM by kscott. 12 replies.

Sort Posts:

  • Using HtmlHelper.Select

    03-11-2008, 12:33 AM
    • Loading...
    • kscott
    • Joined on 03-11-2008, 12:28 AM
    • Posts 5

    I have a need to build a select list box with the contents of a database table. I also need to add a "None" choice into the list. Right now I am doing this by inserting the "None" entry and then looping the collection from the database.

    Is there a way to do this using Html.Select(...)?

     

    Thanks,

    Ken


     

  • Re: Using HtmlHelper.Select

    03-11-2008, 7:27 AM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 9:08 AM
    • Posts 637
    the prompt property of the Select helper will render text in the first Option tag

    <%= Html.Select( "productList", Products, "name", "id", 0, false, new Hash( prompt => "None" ) ) ) %>

    if you're not familiar with the lambdas Hash then its the same as

    new Dictionary<string, object>{ { "prompt", "None" } }
    or
    new RouteValueDictionary{ new { "prompt", "None" } }

  • Re: Using HtmlHelper.Select

    03-11-2008, 12:16 PM
    • Loading...
    • kscott
    • Joined on 03-11-2008, 12:28 AM
    • Posts 5

    Where does this overload of Select  come from? I'm getting an error that no overload of Select takes 7 arguments 

  • Re: Using HtmlHelper.Select

    03-11-2008, 1:01 PM
    • Loading...
    • kscott
    • Joined on 03-11-2008, 12:28 AM
    • Posts 5

     Found the correct override, but I am not getting the new entry at the top. Here is my code:

    <%= Html.Select("countryID", countries, "Name", "Id", (object)(office.Country == null ? String.Empty : office.Country.Id.ToString()), 0, false, new Hash(prompt => "- None -"))%> 

    The (object)(office.Country ....) is for the selected value.

     I get the list with all countries, and the proper one selected, but there is no "- None -" entry at the top.

     

    Any suggestions?
     

  • Re: Using HtmlHelper.Select

    03-11-2008, 3:58 PM
    • Loading...
    • jgo23
    • Joined on 12-06-2007, 5:52 PM
    • Posts 27

    For me, I'm not using the prompt property... I actually didn't know about it so great to know.  But I populate a strongly-typed view data with my list of items from my controller, and then pass it into my view.  I, too, have to populate it with an extra item, in my case, i have a list of timezones, so my default item says "Select a timezone".  I insert that item from my controller, so that my view doesn't have to worry about it.  In fact, I like doing it as so because the controller now has control of what id to provide for this extra item, which causes it to not have to assume what id the view might use for this additional item.  So it looks like this: (I've modified it slightly so it's easier to follow)

    // In my controller code

    public void Register()
    {
        Execute<RegisterViewData>(RegisterGet, RegisterPost); // Execute is a method in my base controller ... it determines which of the passed in methods to call based on the HttpMethod
        // in this case, it will call RegisterGet


    private void RegisterGet(RegisterViewData viewData)
    {
        using (var db = Database)
        {
            List<Timezone> timezones = new List<Timezone> { new Timezone{ ID = 0, DisplayName = "Select a Time Zone" };
            timezones.AddRange((from tz in db.Timezones
                                where tz.Display
                                orderby tz.DisplayPriority descending, tz.ID ascending
                                select tz).ToArray());
            viewData.Timezones = timezones.AsEnumerable();
        }

        RenderView("Register"); // Under the covers, the viewData that was passed into this method is the same view data that is passed to the view
    }

     

    // In my view
    <%= Html.Select(ViewData.TimezoneIDElementId, ViewData.Timezones, "DisplayName", "ID", ViewData.TimezoneID) %> // ViewData is an instance of my strongly typed ViewData

     

  • Re: Using HtmlHelper.Select

    03-11-2008, 5:36 PM
    Answer
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 9:08 AM
    • Posts 637

    Yeah, that's not a good solution, jgo. 

    On many of my screens i need the same list several times. but it makes more sense to use "(None)" for optional fields, and "Please Select..." for mandatory fields. Some times it's even appropriate to have no "prompt", so instead of sending the same list three times, i send it once and use the prompt property.

    The prompt property works. make sure the type you pass in is either an anonymous type, an IDictionary<string, object> (not a simple IDictionary), or a RouteValueDictionary.

    (I've had to modify the original code for the lambdas Hash object to get this to work cos it used to only derive from HybridDictionary)

  • Re: Using HtmlHelper.Select

    03-11-2008, 5:57 PM
    • Loading...
    • jgo23
    • Joined on 12-06-2007, 5:52 PM
    • Posts 27

    tgmdbm:
    Yeah, that's not a good solution, jgo. 

    Not a good solution for the op's issue, or not at all?
    I think it works very well.  I have my entire site structured so that any data that it needs is retrieved through the ViewData.  I also have a class hierarchy of my ViewData classes, so if I had the same situation as you do, that you need the same list on multiple pages, then I could just move the property up the hierarchy.  As I've mentioned in my previous post, this helps when validating the selected item's value since the controller was responsible for placing that "prompt" item in the list, it knows exactly of its existence (i.e. it's ID).  I don't want my validation logic to assume that 0 (zero) or some arbitrary value is the "prompt" item and not an item from my list.

    I trust that the prompt property works (maybe I don't understand how it fully works), but help me understand why my solution isn't a good idea.  Maybe I'm missing something or you've misunderstood my logic.

  • Re: Using HtmlHelper.Select

    03-11-2008, 7:29 PM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 9:08 AM
    • Posts 637

    jgo23:
    Not a good solution for the op's issue, or not at all?
     

    i'd say not at all, but that's a personal opinion. I would have my list of time zones in ViewData.Timezones and the prompt string in ViewData.TimezonesPrompt or something along those lines.

    I understand your logic cos that's the way i used to do it. But I saw the light.

  • Re: Using HtmlHelper.Select

    03-12-2008, 12:40 AM
    • Loading...
    • kscott
    • Joined on 03-11-2008, 12:28 AM
    • Posts 5
    tgmdbm:

    The prompt property works. make sure the type you pass in is either an anonymous type, an IDictionary<string, object> (not a simple IDictionary), or a RouteValueDictionary.

    (I've had to modify the original code for the lambdas Hash object to get this to work cos it used to only derive from HybridDictionary)

    I got the prompt to work with the Dictionary format. I tried the Hash with lambda but didn't have success. Works well now. Thanks for the help.
  • Re: Using HtmlHelper.Select

    03-17-2008, 9:39 AM
    • Loading...
    • j.strugnell
    • Joined on 05-17-2007, 10:47 AM
    • Posts 9

    Hi tgmdbm,

    Can I ask where/how you discovered this "Prompt" functionality? It's very useful and I'm now wondering what other secrets are waiting to be discovered? Is it documented anywhere?

  • Re: Using HtmlHelper.Select

    03-17-2008, 10:24 AM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 9:08 AM
    • Posts 637

    Someone on these forums asked for the feature and Rob Conery replied saying that he implemented it as the prompt option

    A quick check in Reflector confirms this.

    Ultimately, there will be documentation. I have no idea when that's coming, hopefully soon. ATM we don't even have complete intellisense comments, and the comments on HttpContect etc. dont appear on HttpContextBase etc. (unless i'm missing something).

  • Re: Using HtmlHelper.Select

    03-17-2008, 11:03 AM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 9:08 AM
    • Posts 637

    Can I just ask, kscott, why have you marked that particular post as the answer?

    Surely, if jgo's approach is best, then it should have been jgo's previous post that you marked as the answer.

    But did you seriously mean to say that his approach is best?

    Something I didn't pick up on the first time i read it.

    jgo23:
    so if I had the same situation as you do, that you need the same list on multiple pages, then I could just move the property up the hierarchy
     

    That is not the situation i have..

    tgmdbm:
    On many of my screens i need the same list several times.

    Meaning, I need to have the same list several times on the same screen but with different prompts. Also, that situation occurs on many of my screens. I just have one List<Products> and a few strings for my prompts in the ViewData. Whereas you would have several lists all repeating the same data.

    Hope that makes it clearer. 

  • Re: Using HtmlHelper.Select

    03-17-2008, 11:56 AM
    • Loading...
    • kscott
    • Joined on 03-11-2008, 12:28 AM
    • Posts 5
    I think I wound up clicking the wrong message as the answer. I meant to click yours that specified to use IDictionary, etc. and that using a lambda requires a code change. I've re-marked the item that got me to the working solution.
Page 1 of 1 (13 items)
Microsoft Communities