Beau D'Amore
Beau D'Amore

Reputation: 3404

Only primitive types or enumeration types are supported in this context using IEnumerable object

I am getting Only primitive types or enumeration types are supported in this context while using all IEnumerable objects... I have no idea what is wrong here. Here's my models:

public class UserRoleViewModel
{
    public string Name { get; set; }
    public bool IsChecked { get; set; }

}
public class UserRoleEditViewModel
{

    [DisplayName("Id")]
    public int Id { get; set; }

    [DisplayName("User Name")]
    public string UserName { get; set; }

    [DisplayName("User Roles")]
    public IEnumerable<UserRoleViewModel> UserRoles { get; set; }

}

Here's the Controller:

 public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        User user = db.Users.Find(id);
        if (user == null)
        {
            return HttpNotFound();
        }

        UserRoleEditViewModel model = new UserRoleEditViewModel
        {                
            UserName = user.UserName,
            Id = user.Id,
            UserRoles = db.UserRoles.Select(x=> new UserRoleViewModel(){Name = x.RoleName, IsChecked = x.Users.Contains(user)}).AsEnumerable()                
        };

        return View(model);
    }

And the View:

            @foreach (var userRoleViewModel in Model.UserRoles)//<ERR
            {
                <div>
                    @Html.Hidden(userRoleViewModel.Name)
                    @Html.CheckBox(userRoleViewModel.IsChecked.ToString())
                    @Html.Label(userRoleViewModel.Name, userRoleViewModel.Name)
                </div>
            }
        </div>

tried this too:

 @for (int i = 0; i < Model.UserRoles.Count(); i++) //<ERR
            {                    
                <div>
                    @Html.Hidden(Model.UserRoles.ToList()[i].Name)
                    @Html.CheckBox(Model.UserRoles.ToList()[i].IsChecked.ToString())
                    @Html.LabelFor(x => x.UserRoles.ToList()[i].IsChecked, Model.UserRoles.ToList()[i].Name)
                </div>
            }

My viewmodels are all IEnumerable'd... so, I have no idea what is wrong here.

Lines above marked "//

nor

@foreach (var userRoleViewModel in Model.UserRoles)

Upvotes: 0

Views: 182

Answers (2)

Beau D&#39;Amore
Beau D&#39;Amore

Reputation: 3404

FYI, I wound up doing this ugliness instead:

  List<UserRoleViewModel> vmList = new List<UserRoleViewModel>();
        foreach (var userRole in db.UserRoles)
        {
            UserRoleViewModel urVM = new UserRoleViewModel()
            {
                Name = userRole.RoleName
            };

            if (user.UserRoles.Contains(userRole))
                urVM.IsChecked = true;
            else
                urVM.IsChecked = false;

            vmList.Add(urVM);
        }

        model.UserRoles = vmList;

Upvotes: 0

Matt Burland
Matt Burland

Reputation: 45155

I'm assuming your error is probably here:

UserRoles = db.UserRoles.Select(x=> 
    new UserRoleViewModel(){Name = x.RoleName, IsChecked = x.Users.Contains(user)})
    .AsEnumerable() 

The problem is that you can't translate that in SQL. There's no SQL statement that will construct your UserRoleViewModel object. So what you'll need to do is to select the data you need from the database first, and then use that to construct your object. For example, something like this:

// You can create anonymous types in LINQ to SQL, so do that first
UserRoles = db.UserRoles.Select(x => new { x.RoleName, IsChecked = x.Users.Contains(user) })
    .ToList()    // this forces the database to actually return data
    // Now we can actually construct your object because we aren't doing on the database
    .Select(x => new UserRoleViewModel() { Name = x.RoleName, IsChecked = x.IsChecked });

Upvotes: 1

Related Questions