views:

64

answers:

1

Good day,

I have this problem with Html.DropDownListFor which I can't seem to work out.. It gives me the correct number of options, if I do a breakpoint in the code where it is supposed to render the list, the "SelectItemList" object contains the items with correct values, but the actual HTML it spits out is the following:

<select id="Reason_ReasonId" name="Reason.ReasonId"><option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
</select>

The module containts this:

public SelectList ReasonList
{
    get
    {
        SelectList selectList;
        List<SelectListItem> selectItems;

        using (var db = new EscalationDataContext())
        {
            var reasons =
                db.Reasons
                .OrderBy(r => r.Reason1)
                .Select(r => new SelectListItem
                {
                    Value = r.ReasonId.ToString(),
                    Text = r.Reason1
                });

            selectItems = reasons.ToList();

            selectList = new SelectList(selectItems);
        }

        return selectList;
    }
}

The controller just creates a default instantiation and sets the default value:

    public ActionResult Create()
    {
        EscalationModel model = new EscalationModel
        {
            Reason = new Reason { ReasonId = new Guid("598c28c2-877a-44fa-9834-3241c5ee9355"), Reason1 = "Taken too long" },
            ActionedDate = DateTime.Now
        };

        return View(model);
    } 

Last but not least, the view:

<%: Html.DropDownListFor(m => m.Reason.ReasonId, Model.ReasonList) %>

Any ideas why it behaves like this? As I said, in the actual code (in the view) I have the correct values, but.. It doesn't like me.. Any help would be greatly appreciated, thanks in advance!

A: 

Ok.. It seems you had to specify which variable in SelectListItem was used for "Value" and which was used for "Text"..

selectList = new SelectList(selectItems);

Became..

selectList = new SelectList(selectItems, "Value", "Text");

Which seems to have done the trick!

Robin