tags:

views:

81

answers:

5

I almost have this solved but need a little push.

Here's what I have:

In the database I have a field called active that is a bit field (True/False)

I have placed a dropdownlist on the View form like this:

<%= Html.DropDownList("lstActive", new SelectList((IEnumerable)ViewData["ActiveList"])) %>

In my controller, I simply have this code to generate the True/False in the dropdown:

        List<string> activeList = new List<string>();
        activeList.Add("True");
        activeList.Add("False");

        ViewData["ActiveList"] = new SelectList(activeList);

I want to bind to the field in the database called active and select it in the dropdown. When I view it like this I get this:

alt text

So the questions are these:

Obviously I am not pointing to the Value and Text property but what is that in this case? And how do I select the value that is in the database?

Any help would be appreciated.

A: 

This should clarify:

Drop-down Lists and ASP.NET MVC

For each select list element you need to set the Text and Value properties...

Leniel Macaferi
+1  A: 

Here's a couple of suggestions for you.

First, your DropdownList's name is "lstActive", so if you create a List<SelectListItem> called "lstActive" and pass that back in ViewData, you don't have to do anything fancy with boxing. Then your declaration looks like:

<%= Html.DropDownList("lstActive") %>

easy, huh?

In your controller, you create your List. Here's a method I've used:

    private List<SelectListItem> GetAccounts(User user)
    {
        var items = new List<SelectListItem>();
        foreach (Account account in user.Accounts)
        {
            var item = new SelectListItem();
            item.Text = account.Name;
            item.Value = account.AccountId.ToString();
            if (ActiveAccount == account.AccountId)
                item.Selected = true;
            items.Add(item);
        }
        return items;
    }

Basically, what I'm trying to point out is that you can set a property on your SelectListItem that you wish to be displayed as selected. Here, I'm using my own code for Users and Accounts, but you'd substitute your own data based on your db query.

ChrisW
this worked out for my situation and worked perfectly - thanks.
RJ
+1  A: 

First thing, you're recreating a SelectList the ViewData data, you should declare the DropBox as follows:

<%= Html.DropDownList("lstActive", ViewData["ActiveList"]) %>

Second, instead of creating a generic list on the controller, create a SelectList and add SelectListItems to it:

var activeList = new SelectList 
{
  new SelectListItem { Text = "True", Value = true },
  new SelectListItem { Text = "False", Value = false }
};

ViewData["ActiveList"] = activeList;
Anero
Using a view-specific model and strongly-typed helpers is going to be a much better and easier to implement solution unless you're stuck with an MVC 1.0 solution.
tvanfosson
I liekd this answer also but having just ViewData["ActiveList"] in the view would not compile. Not sure what the deal was with that.
RJ
+2  A: 

First, this is probably better suited to radio buttons, not a select. Second, you really ought to have a view model with a property that is an IEnumerable<SelectListItem> that supplies the values for the select. You can construct that directly in the model.

 var model = new ViewModel();
 model.ActiveList = new List<SelectListItem>
                    {
                        new SelectListItem { Text = "Yes", Value = "true" },
                        new SelectListITem { Text = "No", Value = "false" }
                    };
 model.Active = false; // this will be the default

 return View( model );

Then in your view (strongly-typed to your view model type):

 <%= Html.DropDownListFor( m => m.Active, Model.ActiveList ) %>

Using radio buttons, you can omit the list (since there are only the two choices).

 <%= Html.RadioButtonFor( m => m.Active, true ) %> Yes
 <%= Html.RadioButtonFor( m => m.Active, false ) %> No
tvanfosson
I like this answer but am having a hard time implementing. Your comment about the radio buttons is well taken, I should definitely use radio buttons for this. My problem with this is that I am using Linq to Entities and have a strongly typed entity tied to the table so I am using Product as the strong type. This answer seems right to me but then I will need to somehow include the selectlist in the entity, Product.
RJ
Also when I tried to do the var model = new ViewModel(), VS did not know what ViewModel was. I was probably missing a namespace reference.
RJ
Great tip on the radiobuttons. I glazed over what you were saying about omitting the list. This worked very well ,thanks.
RJ
@RJ - Probably a bad choice of class name, but `ViewModel` would be a class that you would define. It would include the properties from your entity that you need to show/return from the view. You can either build custom logic to convert between your view model and the entity or use something like AutoMapper.
tvanfosson
A: 

One solution could be as follows:

Model:

public class NameValue
{
    public string Name { get; set; }
    public string Value { get; set; }
}

Controller:

string currentActiveValue = myDB.active.ToString(); 
List<NameValue> yesNoList = new List<NameValue> 
                   {      
                        new NameValue { Name = "Yes", Value = "True" },      
                        new NameValue { Name = "No", Value = "False" }      
                   };   

SelectList myActiveList = new SelectList(yesNoList, "Name", "Value", currentActiveValue);
ViewData["ActiveList"] = myActiveList;

View:

div>Is Active: <%= Html.DropDownList("ActiveList") %></div>
Syd