0

I'm getting the following error:

An expression tree may not contain a dynamic operation

Here are the models:

public class ProjectType
{
    public string Project { get; set; }
    public string Name { get; set; }
    public string Server { get; set; }
}

public class ProjectTypesViewModel
{
    public int SelectedId { get; set; }
    public IEnumerable<ProjectType> ProjectTypes { get; set; }
}

Here's the view:

@using BankruptcyDocketGenerator.ViewModels

@{
    ViewBag.Title = "Bankruptcy Docket Generator";
}

@{
    ProjectTypesViewModel projectTypes = (ProjectTypesViewModel)ViewBag.ProjectTypes;
}

@Html.DropDownListFor(x => x.SelectedId, new SelectList(projectTypes.ProjectTypes, "Project", "Name"), "Select one...")

What am I doing wrong?

Thanks, Jay

birdus
  • 7,062
  • 17
  • 59
  • 89

1 Answers1

0

Based on the following answer, seem that you need to have your view strongly typed in order to work with lambda expressions. Since you are using @Html.DropDownListFor, you are using lambda expressions:

Razor View Engine : An expression tree may not contain a dynamic operation

Solution:

Add the following to the top of your view:

@model BankruptcyDocketGenerator.ViewModels.ProjectTypesViewModel

Update the controller to pass in the ViewModel to the view instead of setting it to the ViewBag:

return View(myProjectTypesVm);
Community
  • 1
  • 1
Andy T
  • 10,223
  • 5
  • 53
  • 95
  • Huh. That seems strange to me. I did what you said (and it worked), but I'm still passing the exact same thing into "DropDownListFor". Can you explain why this is the case? – birdus Oct 09 '13 at 19:24
  • 1
    If you don't type your view, then the view will use `dynamic` as the "type". If you do this, then you cannot use lambda expressions, which is what is used in `@Html.DropDownListFor(x => ...)` – Andy T Oct 09 '13 at 19:30