Having difficulty using an ASP.NET MVC ViewBag and DropDownListfor

You could do this:

@Html.DropDownListFor(x => x.intClient, ViewBag.Clients)

But I would recommend you to avoid ViewBag/ViewData and profit from your view model:

public ActionResult Index()
{
    var model = new TestModel();
    model.Clients = new SelectList(new[]
    {
        new { Value = "1", Text = "client 1" },
        new { Value = "2", Text = "client 2" },
        new { Value = "3", Text = "client 3" },
    }, "Value", "Text");
    model.intClient = 2;
    return View(model);
}

and in the view:

@Html.DropDownListFor(x => x.intClient, Model.Clients)

Leave a Comment