Rendering issue using SelectList from manually defined SelectListItems in MVC 2
Asked Answered
P

3

5

I am using ASP.NET MVC 2 (.NET 3.5), and need to manually define what shall be an Options list. When I do so I get a drop down menu, with each of the manual entries reading 'System.Web.Mvc.SelectListItem'.

My view model defines the list as such:

    public SelectList YesNoList
    {
      get
      {
        List<SelectListItem> tmpList = new List<SelectListItem>();
        tmpList.Add(new SelectListItem {Text = "", Value = ""});
        tmpList.Add(new SelectListItem {Text = "Yes", Value = "1"});
        tmpList.Add(new SelectListItem {Text = "No", Value = "0"});
        YesNoList = new SelectList(tmpList,"");
      }
      private set{}
     }

In the view I reference this using the the Html.DropDownList:

Html.DropDownList("FieldName", viewmodel.YesNoList);

What I am expecting to be rendered on the final web page should be like:

<select id="FieldName" name="FieldName">
  <option value=""/>
  <option value="1">Yes</option>
  <option value="0">No</option>
</select>

Instead I get:

<select id="FieldName" name="FieldName">
  <option>System.Web.Mvc.SelectListItem</option>
  <option>System.Web.Mvc.SelectListItem</option>
  <option>System.Web.Mvc.SelectListItem</option>
</select>

I am at a loss, as I cannot figure out why the type is being returned so would appreciate it if anybody could point out to me what is wrong with the viewmodel definition, or point out a better way. I was hesitant to derive the SelectList from collections of C# classes as the SelectList would provide a consistant way to iterate through the values and display text.

Thanks in advance, hopefully somebody can help.

Cheers,

J

Pitchblende answered 17/9, 2010 at 15:54 Comment(1)
FWIW, that "private set" without an implementation shouldn't be there.Apteryx
W
8

A dropdown can handle a List<SelectListItem> too, just send that in stead.

Html.DropDownList("FieldName", viewmodel.YesNoList);

and

public List<SelectListItem> YesNoList
{
  get
  {
    List<SelectListItem> YesNoList = new List<SelectListItem>();
    YesNoList.Add(new SelectListItem {Text = "", Value = ""});
    YesNoList.Add(new SelectListItem {Text = "Yes", Value = "1"});
    YesNoList.Add(new SelectListItem {Text = "No", Value = "0"});
    return YesNoList;
  }
  private set{}
 }

you are actually doing it wrong on making the selectlist.

it should be:

new SelectList(tmpList, "Value", "Text"); 

and then forget my above code. you can do this with any List, if you give it the list and the value and text "key"

Wheal answered 17/9, 2010 at 16:1 Comment(3)
Fantastic! Thank you, it works! Why it does, I am still unsure - I thought that SelectList and SelectListItem were meant for eachother. Anyway, this has done the trick, and has definetly set the wheels rolling again. Cheers, and thanks for the speedy reply.Pitchblende
dont forget to set my answer as 'the answer'Wheal
@please edit the answer to return and maybe rename the field to be lowercase.Holster
G
0

You could do it by using an editor template. Call it 'YesNo' and include the following code...

@Modeltype Boolean

@Code
  Dim YesNoList = New List(Of SelectListItem)()
  YesNoList.Add(New SelectListItem() With {.Text = "Yes", .Value = True})
  YesNoList.Add(New SelectListItem() With {.Text = "No", .Value = False})

  Dim list = New SelectList(YesNoList, "Value", "Text", Model)
End Code

@Html.DropDownList("", list)

Then within your model assign a UIHint of 'YesNo' on your property. Which means that now the EditorFor this property will give you a nice Yes/No list that will bind as a boolean.

Goaltender answered 21/6, 2012 at 10:22 Comment(0)
D
0

Try this code:

OdbcDataReader iLRt1 = databaseFunctions.databaseConnection.getFromDatabaseReader("select * from groups order by head");

List<SelectListItem> Hello1 = new List<SelectListItem>();
Hello1.Add(new SelectListItem { Text = "Select All", Value = "Select All" });

while (iLRt1.Read())
{
    Hello1.Add(new SelectListItem { Text = iLRt1["head"].ToString(), Value = iLRt1["code"].ToString() });}

ViewData["myList2"] = Hello1;
Dreddy answered 1/6, 2013 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.