没有类型为'IEnumerable'的ViewData项具有键'SelectedUserInteractionTypes'

时间:2017-05-23 11:24:22

标签: c# asp.net-mvc model-view-controller ienumerable html.dropdownlistfor

我正在使用Asp.Net,C#和MVC实现一个webapp。

我正在尝试在我的视图中实现DropDownListFor但发生以下错误:

  

没有类型为'IEnumerable'的ViewData项具有该键   'SelectedUserInteractionTypes'。

我已经搜索了一些解决方案,但我仍然没有得到它。

我的视图看起来像:(第一个DropDownListFor工作正常,但第二个没有)

    <thead>
    @using (Html.BeginForm())
    {
    <tr class="filters">
        <th><input type="text" class="form-control" placeholder="Zeit"></th>
        <th>@Html.DropDownListFor(model => model.FirstOrDefault().SelectedUser, Model.FirstOrDefault().AvailableUser, new { @id = "userFilter", @class = "selectpicker", @multiple = "mulitple", data_live_search = "true"})</th>
        <th>@Html.DropDownListFor(model => model.FirstOrDefault().SelectedUserInteractionTypes, Model.FirstOrDefault().AvailableUserInteractionTypes_ID, new { @id = "interactionTypeFilter", @class = "selectpicker", @multiple = "mulitple", data_live_search = "true" })</th>
        <th><input type="submit" value="Filtern" class="btn btn-default" /></th>
        <th><input onclick="test()" type="submit" value="Zurücksetzen" class="btn btn-default" /></th>
    </tr>
    }

我的UserInteractionViewModels看起来像:

        public MultiSelectList AvailableUser { get; set; }

    private List<int> _selectedUser = new List<int>();

    public List<int> SelectedUser
    {
        get { return _selectedUser; }
        set { _selectedUser = value; }
    }

    public MultiSelectList AvailableUserInteractionTypes_ID { get; set; }

    private List<int> _selectedUserInteractionTypes = new List<int>();

    public List<int> SelectedUserInteractionTypes
    {
        get { return _selectedUserInteractionTypes; }
        set { _selectedUserInteractionTypes = value; }
    }

至少这是控制器的Index方法,其中使用了selectedLists

       public ActionResult Index()
    {

        UserInteractionViewModels userInteractionViewModels = new UserInteractionViewModels();
        userInteractionViewModels.AvailableUser = new MultiSelectList(db.AspNetUsers.ToList(), "Id", "Email", userInteractionViewModels.SelectedUser);
        //userInteractionViewModels.AvailableUserInteractionTypes_ID = new MultiSelectList(db.UserInteractionTypes.ToList(), "UserInteractionTypes_Id", "Name", userInteractionViewModels.SelectedUserInteractionTypes);

        List<UserInteractionViewModels> userInteractions = new List<UserInteractionViewModels>();
        string setValue = Request["userFilter"];
        foreach (UserInteraction userInteraction in db.UserInteractions.ToList())
        {
            userInteractions.Add(new UserInteractionViewModels()
            {
                AspNetUser = userInteraction.AspNetUser,
                Time = userInteraction.Time,
                Description = userInteraction.Description,
                SelectedUser = userInteractionViewModels.SelectedUser,
                AvailableUser = userInteractionViewModels.AvailableUser
            });
        }
        //db.UserInteractions.Include(u => u.AspNetUser).Include(u => u.UserInteractionType);

        // Hier: Überprüfen, ob nach einem bestimmten String gefiltert worden ist

        //if (!String.IsNullOrEmpty(searchString))

        if (userInteractionViewModels.SelectedUser.Count() > 0)
        {
            string test2 = userInteractionViewModels.AvailableUser.FirstOrDefault().ToString();
            foreach (UserInteraction userInteraction in db.UserInteractions.Where(x => x.AspNetUser.Id.Equals(userInteractionViewModels.SelectedUser.FirstOrDefault().ToString())))
            {
                userInteractions.Add(new UserInteractionViewModels()
                {
                    AspNetUser = userInteraction.AspNetUser,
                    Time = userInteraction.Time,
                    Description = userInteraction.Description,
                    SelectedUser = userInteractionViewModels.SelectedUser,
                    AvailableUser = userInteractionViewModels.AvailableUser
                });
            }
            //List<UserInteractionViewModels> userInteractions = db.UserInteractions.Where(x => x.AspNetUser.Email.ToLower().Contains(searchString.ToLower())).ToListAsync());
            //userInteractions = userInteractions.Where(x => x.AspNetUser.Email.ToLower().Contains(searchString.ToLower()) || x.Description.ToLower().Contains(searchString.ToLower()) || x.Time.ToString().ToLower().Contains(searchString.ToLower()));
        }
        return View(userInteractions);
    }

有谁知道我能做些什么来解决问题?非常感谢你!

0 个答案:

没有答案