Problem with MVC2 DropDownListFor, only displaying class name

Good afternoon,

I have this problem with Html.DropDownListFor, which I cannot work for. This gives me the correct number of options, if I make a breakpoint in the code where it should display the list, SelectItemList "contains elements with the correct values, but the actual text that it spits out is as follows:

<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 contains the following:

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 simply creates a default instance 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);
    } 

And last but not least: view:

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

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

+3
1

. , , SelectListItem "Value" "Text".

selectList = new SelectList(selectItems);

..

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

, , !

+6

Source: https://habr.com/ru/post/1762807/


All Articles